Unlike WinForm, WPF does not have an MDI window, but there can be a "parent-child" relationship between WPF windows.
We will find that the window class exposes a property called Owner, which is readable and writable, and we can guess from the name that it should be used to set the window's parent-child relationship. This property looks easy to use, just assign a window instance, and the real difficulty is that you have to figure out "who is the parent window", "Who is the child window", once you get mixed up, there may be some small problems with the application.
This question is a sister asked me, or how to say that the girl is particularly careful, this gadget is estimated many times we will not notice.
Below, the old week to get a sample, we still "reasonable", with a practical example to see the relationship between the parent window and the child window changes will be different.
The first step is to prepare the main window, usually when a WPF project is created, and the project template generates a main window--mainwindow, so let's use it.
<Windowx:class= "Mydemo.mainwindow"... mc:ignorable= "D"Title= "Parent window"Height= " the"Width= "525"> <StackPanelMargin= " the"> <TextBlockMargin= "0,12,0,100"Text= "main Window"FontSize= "Max"FontFamily= "Italic"HorizontalAlignment= "Center"/> <button Content= "Click to sign up for Mars One-way Tour" horizontalalignment= "Center" Padding= "150,15" Click= "OnClick"/> </StackPanel></Window>
Note that button, which is useful later, click it to open another window.
The second step is to create a new WPF window with the following XAML.
<Windowx:class= "Mydemo.addnewwindow"... mc:ignorable= "D"Title= "Registration"Height= "+"Width= "+"> <Grid> <ButtonContent= "Off"VerticalAlignment= "Bottom"HorizontalAlignment= "Center"Padding= "100,10"Click= "OnClick"/> </Grid></Window>
The "Close" button has no special function, just to close the window, the code is as follows.
Private void OnClick (object sender, RoutedEventArgs e) { Close (); }
The third step, back to the main window, just old week reminded that the Button control on the main window, now to use, we handle its click event.
Private void OnClick (object sender, RoutedEventArgs e) { new Addnewwindow (); W2. Show (); This . Owner = w2; }
For a window, you must display it later to manipulate the Owner property, and for security, you can then think about who the owner is after the window is displayed.
Now let's run a little bit. This is the case when you open a new window.
Then, switch to the other window and switch back, and you'll see that the main window is always in front of the new window, even if the new window is active.
Also, you will notice that the main window is closed and the program will not exit, but the program will exit when the new window is closed. That's because W2 is the parent window now, and MainWindow is a child window.
Now let's change the code.
New Addnewwindow (); W2. Show (); // This . Owner = W2; This ;
Watch out, this time it's MainWindow as the parent window, W2 as a child window. This logic meets our requirements, the original expectation is that a new window is opened on the MainWindow window, and when the window is switched back to the application again, you should make the newly opened window active.
Run again, and you'll see that the new window is always blocked in front of the main window.
Well, today's content is almost, or that sentence, you have to figure out who is who's parent window. Give everyone a universal formula:The window referenced by the Owner property becomes the parent window .
". NET deep breaths" parent-child windows in WPF