Mr. Xek
Mr. Xek

Reputation: 91

How to know child window is open or closed ? WPF

In WPF, How do I know if a child window is opened ? my goal is to open only one instance of the window at the same time..

Here is pseudo code in the parent window

if (newWindowIsOpened)    //just a pseudo code
{
newWindow.Close();
newWindow.Show();
}
else{
newWindow.Show();
}

thanks In advance

Upvotes: 1

Views: 1732

Answers (3)

mm8
mm8

Reputation: 169420

In WPF, How do I know if a child window is opened

Look for it in Application.Current.Windows:

var oldWindow = Application.Current.Windows.OfType<YourWindowType>().FirstOrDefault();
if (oldWindow != null)
{
    oldWindow .close();
}
YourWindowType newWindow = new YourWindowType();
newWindow.Show();

Upvotes: 1

Mark Feldman
Mark Feldman

Reputation: 16148

Once a window has been closed you can't show it again. You can repeatedly call Show() and Hide() though, and you can test if it's "open" by checking IsVisible

Upvotes: 0

Ajay Dubedi
Ajay Dubedi

Reputation: 208

You can add below code

public bool newWindowIsOpened;

    public Window14()
    {
        InitializeComponent();

        Window1 window1 = new Window1();
        window1.Closed += new EventHandler(window1_Closed);
        newWindowIsOpened = false;
        window1.Show();
    }

    void window1_Closed(object sender, EventArgs e)
    {
        newWindowIsOpened = true;
    }

Thanks, Ajay Dubedi

Upvotes: 1

Related Questions