How can I close a child window when its parent window is closed? (WPF)

wpf close window
wpf parent window
parent window c#
cannot set owner property to a window that has not been shown previously
window owner
c# this owner
showdialog owner

I have a child window that is created and displayed via Show(). The user may close the main application window, but my child window is still displayed. How can I handle this situation? My preference would be to handle this all from the child window's constructor, as this window may be instantiated from a number of locations. In other words, I don't want to have to add to the main window's "Closing" event handler, code specifically to close this child window, e.g.:

_mainWindow.Closed += (_, __) => _childWindow.Close();

This seems like a pretty typical use-case to me so I'm surprised not to find an answer easily. Perhaps I'm approaching this from the wrong angle?

You could use the Windows.Owner property.

_childWindow.Owner = _mainWindow;

This automatically adds the behaviour of when you close the mainWindow, the childWindow will be closed. However it also comes with other behaviours which you may or may not want. Read more here:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.window.owner?view=netframework-4.7.2

If you don't want these additional behaviours, you could do basically what you've already done but put it in the childWindows constructor like you want.

public childWindow(Window parentWindow)
{
    parentWindow.Closed += (s, e) => this.Close();
}

If you try to close main window closing, closed or any other event of Child Window you have to face “The process cannot access the file because it  1. You could hook up an event handler to the application's main window's Closed event in the constructor of the child window: public partial class ChildWindow : Window { public ChildWindow() { InitializeComponent(); Window mainWindow = Application.Current.MainWindow; if (mainWindow != null) mainWindow.Closed += (s, e) => Close(); } }

You could hook up an event handler to the application's main window's Closed event in the constructor of the child window:

public partial class ChildWindow : Window
{
    public ChildWindow()
    {
        InitializeComponent();
        Window mainWindow = Application.Current.MainWindow;
        if (mainWindow != null)
            mainWindow.Closed += (s, e) => Close();
    }
}

Or you can set the ShutdownMode of your application to OnMainWindowClose in your App.xaml.cs class as suggested by @ChristianMurschall:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ShutdownMode = ShutdownMode.OnMainWindowClose;
    }
}

Examples. The following example demonstrates a Window that determines whether it needs user intervention to close. <Window x:Class="CSharp.​DataWindow"  Introduction. In our WPF application, we call any child window in our event, examples are button click event or other click event. When we call a child window from our main window, normally it loads our main window. But if we click on that event again, it will load again in the main window.

Rather than handling this from the child window, and having to repeat that code for every new child window you might create, why not keep track of when the main window closes from the Application class (in App.xaml.cs), and then also close all child windows? That way, the child window doesn't have to keep any references to the parent window, and you can do it all from the central App class.

Something like

private void Application_Startup(object sender, StartupEventArgs e)
{
    MainWindow mainWindow = new MainWindow();
    mainWindow.Closing += MainWindow_Closing;
}

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    // close all child windows.
}

When opening a new window using JavaScript you can make use of the “Dependent = yes” property which makes the child window close when its parent is closed. When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window. This means that: The child window does not have a reference to the parent window. The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be minimized, maximized, and restored independently of the other.

It does not matter from where it is called from, from a method of what window, or not a window (as soon as it is called from the same UI thread). However, user interaction with the parent window is not possible as long as the child window is open. By default, a user can close a child window by clicking the close button in the title bar, or by pressing the CTRL+SHIFT+F4 key combination. To close a child window programmatically, you can either set the DialogResult value or call the Close method.

The parent window lost focus after closing a child window after closing The MainWindow will lose focus when close the sub window in WPF. A Window can be closed using one of several, well-known, system-provided mechanisms located in its title bar, including: ALT+F4. System menu | Close. Close button. A Window can also be closed using one of several well-known mechanisms within the client area that are provided by developers, including: File | Exit on a main window.

I am developing a WPF application. I wanted to know how can I capture child window closed event in parent window? To prevent a form from closing, handle the Closing event and set the Cancel property of the CancelEventArgs passed to​  My Windows Store app is still running in debug mode after I close it. VS2012 Debugging fails on subsequent runs. WPF Main application stops responding if a child window is being moved around.

Comments
  • You can set the ShutdownMode of your application to OnMainWindowClose. The default behaviour is OnLastWindowClose
  • Hm, I like the owner idea but it seems my issue is not having easy access to the parent window. How might I identify and use the parent window from a control? This might be a really simple question...
  • @sean Its hard to say without knowing more about how your code is set up. If you're calling from within a UserControl or something try Window.GetWindow(this)?
  • Going with Owner = Application.Current.MainWindow in the child window's constructor. I prefer this owner-ship idea for handling this sort of maintenance issue.