首页 新闻 会员 周边

WPF 如何只关闭子窗口

0
悬赏园豆:30 [待解决问题]

为什么我在关闭子窗口时,连同父窗体也同时关闭了?我的代码如下:

父窗体:

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

}    

}

}

小豆豆(普华)的主页 小豆豆(普华) | 初学一级 | 园豆:140
提问于:2011-11-15 06:33
< >
分享
所有回答(1)
0

你那是用户控件。那是什么父窗体啊。

悟行 | 园豆:12559 (专家六级) | 2011-11-15 13:17

哎呦,我弄错了。呵呵...

支持(0) 反对(0) 小豆豆(普华) | 园豆:140 (初学一级) | 2011-11-15 15:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册