Reputation: 91
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
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
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
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