'Check if WPF window is open

How can I check wheher a window is open or not. Is it possible?

For example:

if (window.IsOpen)
{
    // window is open
}
else
{
    // window is not open
}


Solution 1:[1]

To check if a window is shown in the current application:

if(System.Windows.Application.Current.Windows.Contains(yourWindow)) {
    // the window has been shown
} else {
    yourWindow.Show();
}

To check if a window is not minimized:

if(yourWindow.WindowState != WindowState.Minimized) {
    // the window is currently not minimized
} else {
    yourWindow.WindowState = WindowState.Normal;
    // or WindowState.Maximized
}

Solution 2:[2]

You could retain a reference to the window when it's opened. In which case, you don't really need to go find it in the windows. Otherwise, you'll have to get it based on the type of the given window.

        Window1 instance = Application.Current.Windows.OfType<Window1>().SingleOrDefault();
        if (instance !=null)
        {
            instance.Show();
        }

If this can be one of several types of window types and you need a generic approach then it's a bit more fiddly. Here I have a fixed value for my type but you'd want a button per window, each with the type of that window.

        Type specificType = typeof(Window1);
        Window instance = null;
        foreach (Window win in Application.Current.Windows)
        {
            if(win.GetType() == specificType)
            {
                instance = win;
            }
        }
        if (instance != null)
        {
            instance.Show();
        }

Solution 3:[3]

try
{
    if(Application.Current.Windows.OfType<YourType>().Any())  
    {
        var = 1;
    }
    else
    {
        var = 0;
    }
            
}
catch
{

}  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GregorMohorko
Solution 2 Andy
Solution 3 DiogoA.