How do I know if a WPF window is opened
You can check if m_myWindow==null
and only then create and show window. When the window closes set the variable back to null.
if (this.m_myWindow == null)
{
this.m_myWindow = new MyWindow();
this.m_myWindow.Closed += (sender, args) => this.m_myWindow = null;
this.m_myWindow.Show();
}
In WPF
there is a collection of the open Windows
in the Application
class, you could make a helper method to check if the window is open.
Here is an example that will check if any Window
of a certain Type
or if a Window
with a certain name is open, or both.
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
Usage:
if (Helpers.IsWindowOpen<Window>("MyWindowName"))
{
// MyWindowName is open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>())
{
// There is a MyCustomWindowType window open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>("CustomWindowName"))
{
// There is a MyCustomWindowType window named CustomWindowName open
}