Reputation: 157
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.
Upvotes: 0
Views: 8493
Reputation: 693
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());
}
Upvotes: 2
Reputation: 1155
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;
}
Upvotes: 0