Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close all the windows when Mainwindow closes

Tags:

c#

.net

wpf

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?

like image 897
Zoya Sheikh Avatar asked Aug 14 '13 22:08

Zoya Sheikh


People also ask

How do I close all windows in WPF?

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.

How do I close the child window in WPF?

You can set the result from within the ChildWindow using the following code: this. DialogResult = true; // This will automatically close the ChildWindow.


1 Answers

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>
like image 161
J45 Avatar answered Oct 25 '22 14:10

J45