Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】

时间:2023-01-28 07:30:12

【更新备注】感谢朋友们的评论,我修改了例程,使用子窗口Close Event来返回参数到父窗口,希望能够对新手有帮助。

 

在企业级项目中,子窗口(ChildWindow)是一个常用控件,其展示方式是以弹出窗口来显示信息。 这里我将演示,子窗口传递参数到父窗口的方法。由于我的开发环境都是英文环境,所以部分中文可能显示不正常,请大家见谅。


我们的目的是希望用户在子窗口输入一串文字,然后点击提交后,字符串将被返回显示在父窗口。 

 

1. 首先创建一个新项目 “SLChildWindow",

 

Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】 

2. 然后在新项目中,右键点击添加,添加一个新项目,选择“子窗口”(ChildWindow), 改名为"ChildWindowDemo.xaml",添加完成后,在子窗口中添加一个文本框,名为 txtUserInfo,

  

Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】

 

 

 1 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】< controls:ChildWindow  x:Class ="SLChildWindow.ChildWindowDemo"
 2 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】           xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
 3 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】           xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"  
 4 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】           xmlns:controls ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
 5 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】           Width ="400"  Height ="300"  
 6 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】           Title ="ChildWindowDemo" >
 7 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】     < Grid  x:Name ="LayoutRoot"  Margin ="2" >
 8 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】         < Grid.RowDefinitions >
 9 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】             < RowDefinition  />
10 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】             < RowDefinition  Height ="Auto"   />
11 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】         </ Grid.RowDefinitions >
12 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】
13 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】         < TextBox  x:Name ="txtUserInfor"  Grid.Row ="0"   />
14 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】
15 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】         < Button  x:Name ="CancelButton"  Content ="Cancel"  Click ="CancelButton_Click"  Width ="75"  Height ="23"  HorizontalAlignment ="Right"  Margin ="0,12,0,0"  Grid.Row ="1"   />
16 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】         < Button  x:Name ="OKButton"  Content ="OK"  Click ="OKButton_Click"  Width ="75"  Height ="23"  HorizontalAlignment ="Right"  Margin ="0,12,79,0"  Grid.Row ="1"   />
17 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】     </ Grid >
18 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】 </ controls:ChildWindow >

 


3. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,

Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】代码
public   string  TestString{  get set ; }

 

4. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKButton_Click,CancelButton_Click; 在OKButton_Click中将用户输入信息赋值给TestString属性,

 Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】代码

1  private   void  OKButton_Click( object  sender, RoutedEventArgs e)
2          {
3              TestString  =  txtUserInfor.Text;
4               this .DialogResult  =   true ;
5          }

 

 

6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,

 

Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】private  ChildWindowDemo childWindowDemo  =   new  ChildWindowDemo();

 

 

7. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,

Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】Silverlight子窗口(ChildWindow)传递参数到父窗口演示【更新】代码
 1  public  MainPage()
 2          {
 3              InitializeComponent();
 4              childWindowDemo.Closed  +=   new  EventHandler(childWindowDemo_Closed);
 5          }
 6 
 7           private   void  childWindowDemo_Closed( object  sender, EventArgs e)
 8          {
 9               bool ?  result  =  childWindowDemo.DialogResult;
10 
11               if  (result.HasValue  &&  result.Value)
12              {
13                  tbInfo.Text  =  childWindowDemo.TestString;
14              }
15          }
16 
17           private   void  childWindowDemo_OkClicked( object  sender, EventArgs e)
18          {
19              tbInfo.Text  =  childWindowDemo.TestString;
20          }
21 
22           private   void  btPopup_Click( object  sender, RoutedEventArgs e)
23          {
24              childWindowDemo.Show();

25         } 

 

8. 最后通过子窗口属性获取子窗口用户输入信息。 

 

实例演示:  http://silverlightchina.net/html/tips/2009/1125/261.html

代码下载:  http://silverlightchina.net/uploads/soft/091126/1-091126105U8.rar