2

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

Ahmed
  • 45
  • 1
  • 9
  • No that's not the way to do it Ahmed ! You should use `UserControl` or `Pages` for this purpose – Hammas Apr 02 '20 at 16:16
  • Look at this tut : https://www.wpf-tutorial.com/usercontrols-and-customcontrols/creating-using-a-usercontrol/ – Hammas Apr 02 '20 at 16:17
  • and this https://stackoverflow.com/questions/29952693/wpf-usercontrol-loading-dynamically-based-on-which-button-is-clicked – Hammas Apr 02 '20 at 16:18
  • A `UserControl` is just like a `window` without frame (by default) ok and you can Instantiate and use it just like a window. But unlike Window you can Embed a `userControl` inside Window. Hope it helps :) – Hammas Apr 02 '20 at 16:20
  • There's also the Popup control that creates a short term dialog that is completely UI based. https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/popup-overview – Phoenix Stoneham Apr 02 '20 at 16:21

1 Answers1

1

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");
BionicCode
  • 1
  • 4
  • 28
  • 44