I am glad that you received your answer, but for the sake of others I will answer your question to add some information.
Step 1
Firstly, if you want your program to exit when the main window closes, you need to specify, as these are not WinForms, where this is the default behavior.
(The default value in WPF when closing the last window)
In code
Go to the instance of your application at your entry point. (In the VS $ WPF program, by default it is nested inside App.xaml , so go inside and go to App.xaml.cs and create a constructor).
In the constructor, specify that Application ShutdownMode should be ShutdownMode . OnLastWindowClose .
public App() { ShutdownMode = ShutdownMode.OnLastWindowClose; }
In xaml
Go to your App.xaml file (or create one yourself) The root is Application , specify inside the Application ShutdownMode should be ShutdownMode . OnLastWindowClose .
<Application x:Class="WpfApplication27.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" ShutdownMode="OnMainWindowClose">
If this works, everything is ready; you can stop reading.
Step 2
If the above did not work (I think you wrote the WPF application from scratch), the main window is probably not known to the application as the main window. Therefore, indicate this as-well.
In code
Go to the application designer, as in step 1, and specify that Application . MainWindow is Window :
MainWindow = mainWindow;
In xaml
Go to Application XAML, as in step 1, and specify that Application . MainWindow Window value:
MainWindow = "mainWindow";
Alternative
I don't think this is the best approach, simply because WPF does not want you to do this (therefore it is Application ShutdownMode ), but you can just use the event / override the event method (OnEventHappened).
Go to the file with the code below, and then add:
protected override void OnClosed(EventArgs e) { base.OnClosed(e); App.Current.Shutdown(); }