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

13 בינואר 2011

תגיות: , , ,
9 תגובות

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).

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

9 תגובות

  1. Tomer Shamam14 בינואר 2011 ב 11:11

    Settings Window.Topmost to true, solves this problem.

    להגיב
  2. Tal Pasi20 בינואר 2011 ב 22:58

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

    להגיב
  3. Regev3 בפברואר 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.

    להגיב
  4. Tal Pasi4 במרץ 2011 ב 23:52

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

    להגיב
  5. Towhid16 ביולי 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.

    להגיב
  6. BenJoe4 באוגוסט 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!

    להגיב
  7. WoAx6 בפברואר 2012 ב 9:17

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

    להגיב
  8. Naveen23 בנובמבר 2012 ב 1:19

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

    להגיב
  9. LypeLienvoinI4 באפריל 2013 ב 12:02

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

    להגיב