我们的目的是希望用户在子窗口输入一串文字,然后点击提交后,字符串将被返回显示在父窗口。
1. 首先创建一个新项目 “SLChilDWindow",
2. 然后在新项目中,右键点击添加,添加一个新项目,选择“子窗口”(ChilDWindow),改名为"ChilDWindowDemo.xaml",添加完成后,在子窗口中添加一个文本框,名为 txtUserInfo,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 < controls:ChilDWindow x:Class ="SLChilDWindow.ChilDWindowDemo"
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:controls ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls" 5 WIDth ="400" Height ="300" 6 Title ="ChilDWindowDemo" >
7 GrID x:name ="LayoutRoot" margin ="2" 8 GrID.RowDeFinitions 9 RowDeFinition />
10 RowDeFinition Height ="auto" 11 </ 12
13 TextBox ="txtUserInfor" GrID.Row ="0" 14
15 button ="Cancelbutton" Content ="Cancel" Click ="Cancelbutton_Click" WIDth ="75" ="23" HorizontalAlignment ="Right" ="0,12,0" ="1" 16 ="OKbutton" ="OK" ="OKbutton_Click" 17 GrID 18 controls:ChilDWindow >
3. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 string testString;
2
3 public string TestString
4
5 @H_717_301@{
6
7 get {return testString;}
8
9 set { testString = value; }
10
11 }
12
4. 在子窗口中,建立一个EventHandler,父窗口将用该事件控制器获取子窗口参数,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 event EventHandler OkClicked;
5. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKbutton_Click,Cancelbutton_Click; 在OKbutton_Click中调用前面我们定义的时间控制器OkClicked,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 private voID OKbutton_Click( object sender, RoutedEventArgs e)
2 {
3 if (OkClicked != null)
4 {
5 TestString = txtUserInfor.Text;
6 OkClicked(this, new EventArgs());
7 }
8 this.DialogResult = true;
9 }
6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> private ChilDWindowDemo chilDWindowDemo = new ChilDWindowDemo();
7. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public MainPage()
2 {
3 InitializeComponent();
4 chilDWindowDemo.OkClicked += new EventHandler(chilDWindowDemo_OkClicked);
5 }
6
7 voID chilDWindowDemo_OkClicked( 8 {
9 tbInfo.Text = chilDWindowDemo.TestString;
10 }
11
12 voID btPopup_Click( 13 {
14 chilDWindowDemo.Show();
15 }
16
8. 最后通过子窗口属性获取子窗口用户输入信息。
实例演示: http://silverlightchina.net/html/tips/2009/1125/261.html
代码下载: @L_502_1@
总结以上是内存溢出为你收集整理的Silverlight子窗口(ChildWindow)传递参数到父窗口演示全部内容,希望文章能够帮你解决Silverlight子窗口(ChildWindow)传递参数到父窗口演示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)