How to close all windows when Mainwindow closes

I am working on WPF. When the application starts, Mainwindow opens. There are two buttons in this window. Each opens a new window. For example, there are add and update buttons. The "Add" button opens the "Add Element" window when a click event is called and, by a similar update, opens the "Update Element" window. If I close Mainwindow, the two Add-Item and Update-Item windows remain open. I want that if I close Mainwindow, these two other windows should also be closed.

app.current.shutdown 

app.current.shutdown is mainly used. My question is: where should I publish this line of code in my program, in mainwindow or in App.config. Should I call any event or function in this answer too?

+4
source share
4 answers

Install Application.MainWindow in an instance of your main window and make sure Application.ShutdownMode is OnMainWindowClose .

Also, if you do not want the entire application is closed: Take MainWindow Owner of child windows. (This has other side effects)

+6
source

I think the solution is to set the Owner property of each of the sub-windows to the main window. Thus, any window action performed in the main window is also performed in all other windows:

http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx

+2
source

Since at first glance it’s quite difficult to understand the right way to do what our friends advised, I will pass on with a small example (best practice) of the code .

This is what Josh Smith shows what your App.xaml.cs looks like.

 namespace MyApplication { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { static App() { // Ensure the current culture passed into bindings is the OS culture. // By default, WPF uses en-US as the culture, regardless of the system settings. // FrameworkElement.LanguageProperty.OverrideMetadata( typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var window = new MainWindow(); // To ensure all the other Views of a type Window get closed properly. ShutdownMode = ShutdownMode.OnMainWindowClose; // Create the ViewModel which the main window binds. var viewModel = new MainWindowViewModel(); // When the ViewModel asks to be closed, // close the window. EventHandler handler = null; handler = delegate { viewModel.RequestClose -= handler; window.Close(); }; viewModel.RequestClose += handler; // Allow all controls in the window to bind to the ViewModel by // setting the DataContext, which propagates down the element tree. window.DataContext = viewModel; window.Show(); } } } 

Again, in the end, you need how you want to host your MVVM application.

0
source

You can also install ShutdownMode in the App.xaml file:

 <Application x:Class="WpfApp1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" StartupUri="MainWindow.xaml" ShutdownMode="OnMainWindowClose"> <Application.Resources> </Application.Resources> </Application> 
0
source

Source: https://habr.com/ru/post/1497127/


All Articles