I am working on WPF. There is Mainwindow which opens as application starts. There are two buttons within this window. Each open a new window. e.g There are Add and update buttons. Add button opens a Add-Item window on its click event call and similarly update opens it's window "Update-Item". If i close Mainwindow these two windows "Add-Item" and "Update-Item" remains open. I want that if i close Mainwindow, these other two windows should also close with it.
app.current.shutdown
app.current.shutdown is used mostly. My Question is: Where i have to post this code line in my program, in mainwindow or in App.config. Should i have to call any event or function in it's response too?
The proper way to shutdown a WPF app is to use Application. Current. Shutdown() . This will close all open Window s, raise some events so that cleanup code can be run, and it can't be canceled.
You can set the result from within the ChildWindow using the following code: this. DialogResult = true; // This will automatically close the ChildWindow.
You can also set 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With