How do you tell if a WPF Window is closed?

Wpf

Wpf Problem Overview


I'm working on an application that displays some child windows which can either be closed by the user or are automatically closed. While debugging some exceptions that were being thrown, I discovered I was trying to call methods like Hide() on a window that had already been closed; this particular branch of code was common to both cases and I hadn't noticed this.

One of my first ideas was to look for a property on Window that would indicate the window had been closed. I can't seem to find one. In WinForms, I'd look to the IsDisposed property for a somewhat reliable indicator that the form had been closed (it won't reliably work for a dialog but I'm not working with dialogs.) I don't see anything equivalent on Window. The documentation for Window.Close() doesn't seem to indicate any properties that are changed by the method. Am I missing something obvious, or is the only method to know if a window's been closed to handle the Closed event? That seems kind of a harsh requirement for a simple task.

Wpf Solutions


Solution 1 - Wpf

According to this conversation on the MSDN WPF forums (see the last post), you can check to see if the IsLoaded is false, which means that the window is "eligible" for unloading its content. I hope that works for you!

Solution 2 - Wpf

If you derive from the Window class, you can do this:

public bool IsClosed { get; private set; }

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);
    IsClosed = true;
}

It has an advantage over registering for the Closed event - no need to un-register the callback.

Solution 3 - Wpf

Hope this is useful for you:

PresentationSource.FromVisual(window) == null;

Solution 4 - Wpf

My solution was to simply attach an event to the dialog's Closed event:

MikesDialog dlg = new MikesDialog();
dlg.Closed += delegate
{
	//  The user has closed our dialog.
	validationgDlg = null;
};

//  ...elsewhere in the code...

if (validationgDlg != null)
{
     //  Our "MikesDialog" is still open...
     . . .
}

Solution 5 - Wpf

Another way : Application.Windows contains a list of open windows. You can check is this collection contains your window (it is removed after closing).

Looks like you have to call OfType<Window>() because it is a specialized collection.

Solution 6 - Wpf

I don't know why the IsDisposed property is internal, but if you don't fear reflection:

var window = new Window();
var propertyInfo = typeof(Window).GetProperty("IsDisposed", BindingFlags.NonPublic | BindingFlags.Instance);
var isDisposed = (bool)propertyInfo.GetValue(window);

That being said, reflection is not to be overused because you're no longer protected by the public API of the class. Be sure to use at least unit tests if you go that route.

Solution 7 - Wpf

You can add a non static property to the WindowClass bool IsClosed, and set true on the Closed event:

public partial class MyWindow : Window
{
    public bool IsClosed { get; set; } = false;
    public MyWindow()
    {
        Closed += MyWindow_Closed;
        InitializeComponent();
    }
}    

private void MyWindow_Closed(object sender, EventArgs e)
{
   IsClosed = true;
}

Solution 8 - Wpf

In some occasions checking Application.Current.MainWindow.IsActive is enough

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOwenPView Question on Stackoverflow
Solution 1 - WpfscwagnerView Answer on Stackoverflow
Solution 2 - WpfTomView Answer on Stackoverflow
Solution 3 - WpfdexiangView Answer on Stackoverflow
Solution 4 - WpfMike GledhillView Answer on Stackoverflow
Solution 5 - WpfSimon_WeaverView Answer on Stackoverflow
Solution 6 - WpfAdrian RusView Answer on Stackoverflow
Solution 7 - WpfMayer SpitzView Answer on Stackoverflow
Solution 8 - WpfIcadView Answer on Stackoverflow