WPF 如何只关闭子窗口

tongfangshitong 2011-11-15 11:13:46
为什么我在关闭子窗口时,连同父窗体也同时关闭了?我的代码如下:

父窗体:

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(); }

}

}

}

...全文
363 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tony萧 2011-11-25
  • 打赏
  • 举报
回复
一般不会出现这种情况,你是否使用了多线程?

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧