I am new to WPF , i can't find a way to open a new WPF window on the same main WPF app i tried Frame method , here is the code :-
<Window x:Class="WPF_FINAL.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:WPF_FINAL"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
Height="768"
Width="1366"
WindowState="Maximized"
Title="MainWindow">
<Grid Background="#dff9fb"
Margin="33,10,-33,-10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="13.5" />
<ColumnDefinition Width="152" />
<ColumnDefinition Width="auto"
MinWidth="335.5" />
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Frame Margin="0,0,0.5,10"
Grid.ColumnSpan="5"
x:Name="main"
Grid.RowSpan="6">
</Frame>
</Grid>
</Window>
cs code
main.Content = new Window1();
but when i run it gives me break exception i tried also navigation service but i found it's only associated with Pages any suggestion how to do this ? thank you
A Frame
can host any content, even HTML.
The Page
only exposes special helpers like the NavigationService
to make navigation between pages more convenient.
A Window
can not be the child of another element e.g. child of Frame
. It must be the root element. By assigning the Window
to Frame.Content
the Frame
beceomes the parent of the Window
, which is illegal.
A simple solution would be to convert the Window1
class to a UserControl
:
<UserControl x:Class="MyUserControl">
<TextBlock Text="TEST CONTROL" FontSize="25"/>
</UserControl>
Now your assignment will work:
main.Content = new MyUserControl();
or
main.Navigate(new MyUserControl());
or
main.Navigate("file path to/MyUserControl.xaml");