In Win UI 3 I want to change the content of the MainWindow when the user clicks a button. Right now I have the MainWindow with all its content, then the user clicks a button and a new window opens, let's call it SecondWindow. I do this with:
Window secondWindow= new SecondWindow();
secondWindow.Activate();
But what I want is for SecondWindow's content to open in the main window, in place of MainWindow's content. How can I do this?
CodePudding user response:
If you want the display the contents of the secondWindow
inside another window, it should be a UserControl
instead of a window.
You could then simply add an instance of it as a child of the root panel of the main window:
MainWindow.xaml:
<Window ...>
<Grid x:Name="root">
...
MainWindow.xaml.cs:
this.grid.Children.Clear();
this.grid.Children.Add(new YourUserControl());
If you define the contents directly in a window, you should add its Content
to the main window like this:
Window secondWindow = new SecondWindow();
var root = secondWindow.Content;
this.grid.Children.Clear();
this.grid.Children.Add(root);
secondWindow.Close();