8,735
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="221,139,0,0" VerticalAlignment="Top" Width="75" Click="button_Click" />
</Grid>
</Window>
private void button_Click(object sender, RoutedEventArgs e)
{
TestWindow testWindow = new TestWindow();
}
<Window x:Class="WpfApplication3.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="TestWindow" Height="300" Width="300">
<Grid>
</Grid>
</Window>
private void button_Click(object sender, RoutedEventArgs e)
{
TestWindow testWindow = new TestWindow();
testWindow.ShowDialog();
}
testWindow.Owner = System.Windows.Application.Current.MainWindow;
因为:
• When a child window is opened by a parent window by calling ShowDialog, an implicit relationship is established between both parent and child window.
• When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window.
• To allow you to create a relationship between a child window and a parent window, Window supports the notion of ownership. Ownership is established when the Owner property of a window (the owned window) is set with a reference to another window (the owner window).
• If you close an owner window, its owned windows are also closed.