I have written a WPF program that when user clicked a button, a new window will be popped up.
I have tried to show the new window by using Show() or ShowDialog() function.
In Windows 7, when user closed the child window, the main window will remain and the program will not exit. This behavior is what I want to have.
However, when the program is run in Windows XP, when user closed the child window, the main window will be closed together and the whole program will be exited.
I have tried to set different value in different properties in Window class, finally, I found that the program will not exit only when I set the property "ShowInTaskbar" to "False" in child window.
However, if ShowInTaskbar is set to false, user cannot find the entry in task bar which is not the behavior that I want.
What I want to have is really simple. I just want the program running in Windows XP to have the same behavior as the program running in Windows 7 when user closed the child window (i.e. main window will not exit when user closed the child window). Also, I want to have an entry in task bar for a newly created child window(i.e. ShowInTaskbar = true).
Does anyone have any idea about this problem?
MainWindow
<Window x:Class="ChildWindowTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="OpenChild">Open Child Window</Button>
</Grid>
</Window>
Code For MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OpenChild(object sender, RoutedEventArgs e)
{
ChildWindow child = new 开发者_JAVA百科ChildWindow();
child.Owner = this;
//child.ShowInTaskbar = false; <--- if comment, the program will exit, when child window closed
child.Show();
}
}
Child Window:
<Window x:Class="ChildWindowTest.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ChildWindow" Height="300" Width="300">
<Grid>
</Grid>
Code for Child Window:
public partial class ChildWindow : Window
{
public ChildWindow()
{
InitializeComponent();
}
}
Not an elegant solution at all, but you always can subscribe to Closing event in Application class and cancel application closing in an event handler.
Did u make sure you have childWindow.Owner
set as our MainWindow correctly before calling childWindow.ShowDialog()
?
精彩评论