In my c# Wpf standalone application i created Main Window and added to my project another 5 windows
I put in Main Window a button by name of every window from these 5 windows.
my question is: when i press any button in Main Window..how to show its related window in (Windows Area) in Main Window?? e.g. if i press history button ..how to show History window in that (Windows Area) of Main Window??
Note: i don't want to use pages or navigation windows with navigation controls in my application.
Just use UserControl's and add them to your Container Control, when you click your buttons.
Here is a simplistic example:
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="History" Height="53" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Precaution" Height="53" HorizontalAlignment="Left" Margin="109,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<Button Content="Uses" Height="53" HorizontalAlignment="Left" Margin="208,12,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
<Button Content="Side Effects" Height="53" HorizontalAlignment="Left" Margin="314,12,0,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click" />
<Button Content="New Item" Height="53" HorizontalAlignment="Left" Margin="405,12,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button5_Click" />
<Viewbox Height="209" HorizontalAlignment="Left" Margin="12,90,0,0" Name="Container" VerticalAlignment="Top" Width="479" />
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
History use1 = new History();
Precaution use2 = new Precaution();
Uses use3 = new Uses();
SideEffect use4 = new SideEffect();
NewItem use5 = new NewItem();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Container.Child = use1;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Container.Child = use2;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
Container.Child = use3;
}
private void button4_Click(object sender, RoutedEventArgs e)
{
Container.Child = use4;
}
private void button5_Click(object sender, RoutedEventArgs e)
{
Container.Child = use5;
}
}
Dummy UserControls
<UserControl x:Class="WpfApplication1.History"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="AliceBlue">
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.Precaution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightCoral" >
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.Uses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightGreen" >
<Grid Width="479" Height="209">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.SideEffect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="PapayaWhip" >
<Grid Height="209" Width="479">
</Grid>
</UserControl>
<UserControl x:Class="WpfApplication1.NewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="209" d:DesignWidth="479" Background="LightGoldenrodYellow">
<Grid Height="209" Width="479">
</Grid>
</UserControl>
Related
I need help accessing in C# a TreeView created in .xaml.
I'm creating a simple TreeView that will collect all "Layers" from a document, and populate the TreeView with their names and other properties.
As a start point, I just want the names, then I'll add more properties.
The .xaml is as follows:
<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestAlpha"
mc:Ignorable="d"
Closing="Window_Closing"
Title=" Document Structure" Height="600" MinHeight="600" MaxHeight="600" Width="400" MinWidth="400" MaxWidth="400" ScrollViewer.VerticalScrollBarVisibility="Visible">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
In C# I have:
// all necessary using statements and the two below, that are specific thar I need in this method
using static TestWPFAlpha.DocumentStructureWindow;
using static DocumentLayers.LayerManagerWindow;
...
and an event for a button as below:
public static void DocumentStructure_Click(object sender, RoutedEventArgs e)
{
if (documentStructureWindow == null)
{
documentStructureWindow = new TestWPFAlpha.DocumentStructureWindow();
documentStructureWindow.Show();
}
else
{
documentStructureWindow.Activate();
}
var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();
System.Type canvasType = typeof(System.Windows.Controls.Canvas);
int count = 0;
foreach (UIElement uie in TestWPFAlpha.MainWindow.canvasGrid.Children)
{
if (uie.GetType() == canvasType && count > 0)
{
sb.AppendLine(Layers[count - 1].Name);
TreeViewItem newChild = new TreeViewItem();
// Layers are from DocumentLayers.LayerManagerWindow
newChild.Header = Layers[count - 1].Name;
tds.Items.Add(newChild);
}
count++;
}
}
At line 49
var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();
I get the error:
The type name 'gridDocumentStructure' does not exist in the type 'DocumentStructureWindow'
Help greatly appreciated.
An example as you requested. It's just a basic thing using code behind
the window with the tree view:
<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPFAlpha"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
it creates a new window:
usings...;
namespace TestWPFAlpha
{
public partial class DocumentStructureWindow : Window
{
public DocumentStructureWindow()
{
InitializeComponent();
var newWin = new Window1(this);
newWin.Show();
}
}
}
and the newWin has a button, which does what I think you would like to achieve:
public partial class Window1 : Window
{
private DocumentStructureWindow _documentStructureWindow;
public Window1(DocumentStructureWindow documentStructureWindow)
{
InitializeComponent();
_documentStructureWindow = documentStructureWindow;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var tds = _documentStructureWindow.TreeViewDocumentStructure;
tds.Items.Add(new TreeViewItem() { Header = DateTime.Now.ToString("yyMMdd_HHmmss_fff")} );
}
}
see it here: repo
But I would recommend mvvm & commands instead of this code behind solution
The following code can be used if the value buttockno in Table Al is of a string type.
What should I do if the value of buttockno in table Al is numeric?
<Window x:Class="DamDarYar_IDMS.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DamDarYar_IDMS"
mc:Ignorable="d"
Title="Window4" Height="300" Width="300">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="151,44,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="60,82,0,0" VerticalAlignment="Top" Height="112" Width="211"/>
</Grid>
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
IDMS_dbEntities db = new IDMS_dbEntities();
try
{
dataGrid.ItemsSource = db.tblAIs.Where(a => a.ButtockNo.StartsWith(textBox.Text.Trim())).ToList();
}
catch
{
}
}
}
}
I would recommend using a filter for that but using your method simply convert to string first.
a.NumericProperty.ToString().StartsWith(...
I have two XAML pages and when you click a button in one, the other page opens. My code is:
private void cookingButton(object sender, RoutedEventArgs e)
{
CookingMenu cooking = new CookingMenu();
cooking.Show();
}
CookingMenu is the name of the XAML page. This code opens the page in a new window but is there any way to open the page in the same window? Both the pages are the same size.
Hope this helps:
App.xaml.cs
public partial class App : Application
{
public static MainWindow ParentWindowRef;
}
MainWindow.xaml
<Window x:Class="MultiplePageOpeninSameScreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<Frame Name="ParentFrame"/>
</DockPanel>
</Window>
MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
App.ParentWindowRef = this;
this.ParentFrame.Navigate(new Page1());
}
Page1.xaml
<Page x:Class="MultiplePageOpeninSameScreen.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="TxtBlockSomeTxt" Text="This is Page One" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Grid.Row="1" Name="BtnGoToPageTwo" Content="Go To Page 2" Click="BtnGoToPageTwo_Click" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
</Grid>
</Page>
Page1.xaml.cs
private void BtnGoToPageTwo_Click(object sender, RoutedEventArgs e)
{
App.ParentWindowRef.ParentFrame.Navigate(new Page2());
}
Page2.xaml
<Page x:Class="MultiplePageOpeninSameScreen.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MultiplePageOpeninSameScreen"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="TxtBlockSomeTxt" Text="This is Page Two" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Grid.Row ="1" Name="BtnGoToPageOne" Content="Go To Page One" Click="BtnGoToPageOne_Click" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>
</Page>
Page2.xaml.cs
private void BtnGoToPageOne_Click(object sender, RoutedEventArgs e)
{
App.ParentWindowRef.ParentFrame.Navigate(new Page1());
}
You should have a look at the Frame control here is the MSDN doc
Here is an great example by Paul Stovell you should look into.
Probably the simplest way would be to use the Frame control as others have proposed here but I want to show a different way, it is possible to change the content of a window to another, try this code:
CookingMenu Testing = new CookingMenu();
private void cookingButton(object sender, RoutedEventArgs e)
{
this.Content = Testing.Content;
}
So, I'm getting some weird behavior when getting the current position using the GetPositionAsync method in my app. It seems to be firing the PositionChanged event when I query for the position. I'm not sure exactly what's going on and it's hard to debug since the call is asynchronous. Here's some sample code that illustrates the problem, (you'll need a Bing Maps key to test it):
MainPage.xaml.cs
using System;
using Bing.Maps;
using Windows.UI.Xaml.Input;
using Windows.Devices.Geolocation;
using Windows.UI.Core;
using GeoPositionExercise.Model;
namespace GeoPositionExercise
{
public sealed partial class MainPage
{
private Geolocator _geolocator;
private LocationIcon _locationIcon = new LocationIcon();
public MainPage()
{
InitializeComponent();
MyMap.Children.Add(_locationIcon);
_geolocator = new Geolocator();
//LoadPoly(PolyLayer, DefaultStyle, GetLocation);
_geolocator.PositionChanged += this._geolocator_PositionChanged;
}
private void _geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, new DispatchedHandler(
() =>
{
displayPosition(this, args.Position.Coordinate);
}));
}
private void displayPosition(object sender, Geocoordinate coordinate)
{
Location location = new Location(coordinate.Latitude, coordinate.Longitude);
MapLayer.SetPosition(_locationIcon, location);
MyMap.SetView(location, 15.0f);
}
private async void GetLocation(object sender, TappedRoutedEventArgs e)
{
// This raises a PositionChanged event for some reason
var position = await _geolocator.GetGeopositionAsync();
}
}
}
MainPage.xaml
<common:PageBase xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:common="using:GeoPositionExercise.Common"
xmlns:local="using:GeoPositionExercise"
xmlns:ignore="http://www.ignore.com"
x:Name="PageRoot"
x:Class="GeoPositionExercise.MainPage"
xmlns:m="using:Bing.Maps"
mc:Ignorable="d ignore"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Page.BottomAppBar>
<AppBar>
<AppBarButton Label="Get Location" Tapped="GetLocation">
<AppBarButton.Icon>
<FontIcon Glyph="" />
</AppBarButton.Icon>
</AppBarButton>
</AppBar>
</Page.BottomAppBar>
<Grid x:Name="MainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,53">
<m:Map x:Name="MyMap" Credentials="YOUR BING MAP KEY HERE" >
<m:MapLayer x:Name="PinLayer"/>
<m:MapLayer x:Name="InfoboxLayer" Visibility="Collapsed">
<Grid x:Name="Infobox" Width="350">
<Border Background="Black" Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5"/>
<Grid Margin="5">
<StackPanel HorizontalAlignment="Left" Margin="10">
<TextBlock x:Name="InfoboxTitle" FontSize="18" Width="290" HorizontalAlignment="Left" TextWrapping="Wrap" />
<WebView x:Name="InfoboxDescription" Width="330" Height="100" Margin="0,10,0,0"/>
</StackPanel>
</Grid>
</Grid>
</m:MapLayer>
</m:Map>
</Grid>
</common:PageBase>
And finally a user control LocationIcon:
<UserControl
x:Class="GeoPositionExercise.Model.LocationIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GeoPositionExercise.Model"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Canvas>
<Ellipse Height="25" Width="25" Fill="#FFF0F0F0" Margin="-12.5,-12.5,0,0"></Ellipse>
<Ellipse Height="20" Width="20" Fill="Black" Margin="-10,-10,0,0"></Ellipse>
<Ellipse Height="10" Width="10" Fill="White" Margin="-5,-5,0,0"></Ellipse>
</Canvas>
</UserControl>
Make sure to have the right references and target a X64 or x82 platform before compiling. You can test this by clicking the "Get Location" app bar button, it'll always center the map at the current location. I'm not really sure why this is occuring, since the only place I call displayPosition is in the _geolocator_PostionChanged method.
I have this data grid where I am placing all my buttons
<Grid x:Name="ButtonGrid" HorizontalAlignment="Left" Margin="0,90,0,4" Width="186">
<Button x:Name="B1" Content="B1" Height="18" Margin="73,0,59,16" VerticalAlignment="Bottom" Click="B1"/>
<Button x:Name="B2" Content="B2" Height="18" Margin="0,0,-2,16" VerticalAlignment="Bottom" Click="B2_Click" HorizontalAlignment="Right" Width="57"/>
</Grid>
I have the grid collapased on start. But when a button {testGrid} is clicked, I want the grid to ne visible.
Here is my code
namespace project.Test
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
EDUTED
private void testGrid_Click(object sender, System.Windows.RoutedEventArgs e)
{
FrameworkElement ButtonGrid = (sender as FrameworkElement).FindName("ButtonGrid") as FrameworkElement;
if ( ButtonGrid.Visibility == System.Windows.Visibility.Collapsed)
ButtonGrid.Visibility = System.Windows.Visibility.Visible;
else
ButtonGrid.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
I think if you move your Grid outside of your DataTemplate it will work. :)
However if you really need to put it in a DataTemplate, as long as your Button is at the same level as the Grid, you should still be able to find it.
Say your xaml code looks like this,
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="controlstoryboardactionrefissue.MainPage" Width="640" Height="480">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid x:Name="myGrid" Height="128" Background="#FFE7C0C0" Width="333">
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="31,29,0,0" Click="myButton_Click" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ContentControl HorizontalAlignment="Left" VerticalAlignment="Top" Margin="175,198,0,0" ContentTemplate="{StaticResource DataTemplate1}" />
</Grid>
</UserControl>
Then the code behind,
private void myButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myButton = (Button)sender;
var grid = myButton.Parent as Grid;
if (grid != null)
{
// do stuff
}
}
Hope it helps. :)