I have the following simple wpf application:
App.xaml:
<Application x:Class="TestWpf2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
App.xaml.cs:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var parentWindow = new Window();
parentWindow.Show();
var childWindow1 = new Window { Owner = parentWindow };
childWindow1.Show();
var childWindow2 = new Window { Owner = parentWindow };
childWindow2.Show();
}
}
The application causes 3 windows to appear on screen. If you run the application and close the two child windows, the parent window is minimized to the task bar. If you comment out childWindow2.show()
, run the application, and close the single child window, the parent window is not minimized to the taskbar.
I can add the following code to work around this problem:
childWindow1.Closing += delegate(object sender, CancelEventArgs ex)
{
(sender as Window).Owner = null;
};
but I don't want to use a hack like this, and I want to understand why this problem occurs.
Why does this happen?
parentWindow.Activate()
after having created child windows, it should fix the problem. Child windows steal activation (input) and when no window is activated, another window gets it. – Simon Mourier