Introduction
Modeless windows - they're pretty handy. The things they're handy for are way too numerous to iterate, so you'll just have to take my word for it. The problem with modeless windows is that unless you specifically close them before (or when) the main window closes, they'll stay open even after the main window is closed.
The Solution to the Modeless Problem
The fix is a simple matter of knowing when the main window (or the owning window) is closing. In order to do that, you simply handle the owner's Closing event. I wrote a method that takes care of this chore (you can certainly do it the way that makes sense to you):
protected void HandleModeless()
{
if (!this.IsModal())
{
if (this.Owner == null)
{
this.Owner = Application.Current.MainWindow;
}
if (this.Owner != null)
{
Owner.Closing += this.Owner_Closing;
}
}
}
The event handler is very simple. It simply closes the form.
protected virtual void Owner_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Close();
}
You may have noticed the if condition at the top of the HandleModelss
method.
if (!this.IsModal())
In my case, this is an Window extension method that reads a private Window property that can be used to determine if the window is modal.
public static class ExtendWpfWindow
{
public static bool IsModal(this Window window)
{
return (bool)(typeof(Window).GetField("_showingAsDialog",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window));
}
}
You certainly don't have to use this extension method. In fact, you can do it right in the window's code:
protected void HandleModeless()
{
if (!(bool)(typeof(Window).GetField("_showingAsDialog",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window)))
{
...
}
}
or even put a method in the window's code:
private bool IsModal()
{
return ((bool)(typeof(Window).GetField("_showingAsDialog",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window)));
}
protected void HandleModeless()
{
if (!this.IsModal())
{
...
}
}
I personally think it's more convenient if done with an extension method, especially if you have an assembly with commonly used code, and you don't want to have to remember exactly how to do something that an extension method does for you, not to mention an extension method is more readily re-usable.
It's my humble opinion that Microsoft should have handled this for us in the WPF framework, because this is the kind of stuff developers shouldn't have to concern themselves with except under special circumstances.
History
- 2021.02.27 - Initial publication.
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.