Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Create sibling window and close current one

Tags:

c#

wpf

What I need is such an event handler in my window class.

void someEventHandler(object sender, RoutedEventArgs e)
{
    MyNewWindow mnw = new MyNewWindow();
    mnw.Owner = Window.GetWindow(this);
    mnw.ShowDialog();
    this.Close();
}

Window.GetWindow(this) returns the parent window of the current window.

I had thought when the owner of the new window is the parent window of the current one, it would wait for the parent; and not the current one. But it did not work that way. Current window waits for the execution of the new and closes only after.

If I use Show() instead of ShowDialog() for some reason the window is not shown at all.

Probably I need some delegate methods but I am not sure where to start.

Edit: I guess I need to improve the question for future references: The new window should be a dialog to the parent window. If I use Show() the parent window becomes accesible and I dont want that. If I use ShowDialog() it becomes a dialog to the current window, meaning that the current window does not close until the new window is closed, and I dont want that either.

like image 260
emregon Avatar asked Feb 24 '23 14:02

emregon


1 Answers

Closing a window causes any windows that it owns to be closed.

If you just want the owner window to not be visible, try this;

void someEventHandler(object sender, RoutedEventArgs e)
{
    MyNewWindow mnw = new MyNewWindow();
    mnw.Owner = this;
    this.Hide(); // not required if using the child events below
    mnw.ShowDialog();
}

You'll likely want to hook up some event in the parent window that acts accordingly when you close the child window depending on your requirements.

EDIT

You could perhaps control the hiding of the (multiple parents) from the child;

void OnLoad(object sender, RoutedEventArgs e)
{
  this.Owner.Hide();
}
void Closed(object sender, RoutedEventArgs e)
{
  this.Owner.Show();
}
like image 127
Stafford Williams Avatar answered Mar 03 '23 00:03

Stafford Williams