WPF – Main Window disappears when closing the last owned child window

January 13, 2011

9 comments

Today I witnessed a weird thing with WPF

Itay, a very good friend of mine noticed that when you have multiple child windows in a WPF application, and you close the last one, the application suddenly disappears,

This only happens if you have more than one child window and you set the MainWindow as the owner of the other windows 

This is a very unexpected behavior.

Let me demonstrate

The following code is in the MainWindow.xaml.cs

 

private void ShowChildWindows()
{
    ChildWindow window = new ChildWindow() { Owner = this };
    window.Show();

    window = new ChildWindow() { Owner = this };
    window.Show();
}

Once I close both of the windows mentioned above, the MainWindow is hidden away for some reason.

The workaround to this problem is to detect that you are closing the last window and remove the owner of that child window.

For example:

private void ShowChildWindowsFixedVersion()
{
    ChildWindow window = new ChildWindow() { Owner = this };
    window.Closing += window_Closing;
    window.Show();

    window = new ChildWindow() { Owner = this };
    window.Closing += window_Closing;
    window.Show();
}

void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    var child = sender as Window;

    if (child != null && this.OwnedWindows.Count == 1)
    {
        child.Owner = null;
    }
}

What we see here that I’ve registered to the closing event and then I check if this child window is the only window that is owned by the MainWindow (this).

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

9 comments

  1. Tomer ShamamJanuary 14, 2011 ב 11:11

    Settings Window.Topmost to true, solves this problem.

    Reply
  2. Tal PasiJanuary 20, 2011 ב 22:58

    You are right, but having your MainWindow as TopMost is not always a valid option.

    Reply
  3. RegevFebruary 3, 2011 ב 11:25

    I’ve encountered this issue too. after trying to use
    Tal’s solution i got this exception:

    System.InvalidOperationException: Cannot set Owner property after Dialog is shown.
    are you familiar with that?

    BTW – i’m using .net 3.5 and opening the window using ShowDialog.

    Reply
  4. Tal PasiMarch 4, 2011 ב 23:52

    Hi Regev
    try setting the Owner property before the ShowDialog
    is called.

    Reply
  5. TowhidJuly 16, 2011 ב 7:23

    I also face the same problem. My requirements were… you have to set up ShowInTaskbar property false for every child window. I solve this problem writing Window_Closing event in every child window. Inside Window_Closing events I write

    this.ShowInTaskbar = true;
    this.Owner = null;

    look I set up ShowInTaskbar = true in Window_Closing event, so it doesn’t violate my requirement but solve the problem.

    Reply
  6. BenJoeAugust 4, 2011 ב 12:03

    hello,

    this tip was spot-on and solved theproblem i had with my application’s window dissapearing into the background when child windows were closed.

    great work there :) thank you very much!

    Reply
  7. WoAxFebruary 6, 2012 ב 9:17

    Thx for the tip with the Topmost. This works beste.

    Reply
  8. NaveenNovember 23, 2012 ב 1:19

    Make sure that when closing the window, fire the event on window closed and close the other main windows

    Reply
  9. LypeLienvoinIApril 4, 2013 ב 12:02

    [url=http://www.paulsmithjpshow.com]財布ポールスミス[/url]

    Reply