为什么我在关闭子窗口时,连同父窗体也同时关闭了?我的代码如下:
父窗体:
index.xaml
<UserControl x:Class="WpfOA.Views.BusinessManageView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfOA.FormControl" mc:Ignorable="d" d:DesignHeight="354" d:DesignWidth="746">
<Grid>
<TextBlock Name="textBlock_AddFile" Text="添加业务文件:" />
<Button Click="ButtonClick_ShowChildWin" Content=" 添加 "></Button>
</Grid>
</UserControl>
index.xaml.cs
//打开子窗口
TestChildWin testChildWin = new TestChildWin();
public void ButtonClick_ShowChildWin(object sender, EventArgs e) { testChildWin.WindowStyle = System.Windows.WindowStyle.None;
testChildWin.ChangeTextEvent += new ChangeTextHandler(FuncSettingClosed);
testChildWin.ShowDialog();
}
//设置窗口关闭时执行的方法
public void FuncSettingClosed()
{
MessageBox.Show("================");
testChildWin.Close();
}
子窗体:
TestChildWin.xaml
<Window x:Class="WpfOA.Views.TestChildWin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestChildWin" Height="300" Width="300">
<Grid>
<Button Content="关闭" Click="ButtonClick_Closed" Margin="5,5,0,0" Width="40" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" IsHitTestVisible="True"></Button>
<Button Content="返回" Click="Window_Closed" Margin="50,5,0,0" Width="40" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" IsHitTestVisible="True"></Button>
</Grid>
</Window>
TestChildWin.xaml.cs
namespace WpfOA.Views
{
//定义委托 public delegate void ChangeTextHandler();
/// <summary>
/// TestChildWin.xaml 的交互逻辑
/// </summary>
public partial class TestChildWin : Window
{
//定义事件 public event ChangeTextHandler ChangeTextEvent;
public TestChildWin() { InitializeComponent(); }
private void Window_Closed(object sender, EventArgs e)
{ StrikeEvent();
}
private void ButtonClick_Closed(object sender, EventArgs e)
{ this.Close();
}
//触发事件改变MainWindow的值
private void StrikeEvent()
{
if (ChangeTextEvent != null)
{ ChangeTextEvent(); }
}
}
}
你那是用户控件。那是什么父窗体啊。
哎呦,我弄错了。呵呵...