How do you know if a WPF window is closed?

I am working on an application that displays some child windows that can be closed by the user or automatically closed. When debugging some of the exceptions that were thrown, I found that I was trying to call methods such as Hide() in an already closed window; this particular code branch was common to both cases, and I did not notice this.

One of my first ideas was to look for a property on the Window that would indicate that the window was closed. I can't seem to find him. In WinForms, I would look at the IsDisposed property for a somewhat reliable indicator that the form was closed (it will not work reliably for dialogue, but I do not work with dialog boxes.) I do not see anything equivalent to Window . The documentation for Window.Close() does not appear to indicate any properties that have been modified by the method. Am I missing something obvious or is this the only way to find out if the window is closed to handle the Closed event? This seems like a harsh requirement for a simple task.

+50
wpf
Dec 19 '08 at 19:32
source share
8 answers

According to this talk on the MSDN WPF forums (see the last post), you can check if IsLoaded is false, which means that the window is “authorized” to offload its contents. I hope this works for you!

+53
Dec 19 '08 at 19:53
source share

If you exit the Window class, you can do this:

 public bool IsClosed { get; private set; } protected override void OnClosed(EventArgs e) { base.OnClosed(e); IsClosed = true; } 

This has an advantage over registration for a Closed event - there is no need to uninstall the callback.

+11
Apr 04 '16 at 11:00
source share

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

It looks like you need to call OfType<Window>() because it is a specialized collection.

+7
Dec 07 2018-11-11T00:
source share

I do not know why the IsDisposed property is internal, but if you are not afraid of reflection:

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

Speaking of which, thinking should not be abused because you are no longer protected by the public class API. Be sure to use at least single tests if you go along this route.

+5
Sep 24 '15 at 16:10
source share

Hope this is helpful for you:

PresentationSource.FromVisual (window) == null;

+4
Oct 14 '14 at 8:25
source share

My solution was to simply attach the event to the Closed event dialog:

 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... . . . } 
+4
Oct. 16 '15 at 9:28
source share

You can add a non-static property to the WindowClass bool IsClosed and set true for 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; } 
0
Jan 17 '18 at 10:03
source share

I have a little twist on this issue ...

Development of a form that saves a spreadsheet during the user’s action “send record”. If the spreadsheet is already open, the Save As dialog box opens.

If the user clicks the cancel button in this window, is there a way to detect it?

0
Jul 18 '19 at 11:55
source share



All Articles