几乎所有的业务系统都有弹出窗口,典型场景有二种 :
1、简单的弹出一个对话框显示信息,比如下面这样:
这个很简单,代码示例如下:
DialogParameters pars = new DialogParameters(); |
pars.Content = "Hello World" ; |
2、点击某条记录的“编辑”按钮,传入ID参数,弹出一个窗口,编辑保存后,将操作结果返回给父窗口
这种场景下,要求:
a)弹出窗口能接受到父窗口传过来的参数
b)弹出窗口关闭时,父窗口要能区分出是通过什么操作关闭的(比如:是直接点击右上角的X按钮关的,还是点击“提交”按钮关的,或是点击“取消”按钮关的)
c)弹出窗关闭后,父窗口要能知道操作结果
示例代码如下:
弹出窗口Xaml部分:
01 | <telerik:RadWindow x:Class= "Telerik.Sample.PopWinUserReg" |
08 | d:DesignHeight= "480" d:DesignWidth= "640" WindowStartupLocation= "CenterScreen" Header= "会员注册" Padding= "10" Width= "640" Height= "300" ResizeMode= "NoResize" > |
10 | <Grid x:Name= "LayoutRoot" Background= "White" > |
12 | <RowDefinition Height= "30" /> |
13 | <RowDefinition Height= "30" /> |
14 | <RowDefinition Height= "30" /> |
15 | <RowDefinition Height= "30" /> |
16 | <RowDefinition Height= "30" /> |
17 | <RowDefinition Height= "30" /> |
18 | <RowDefinition Height= "30" /> |
19 | <RowDefinition Height= "Auto" MinHeight= "10" /> |
20 | <RowDefinition Height= "30" /> |
21 | </Grid.RowDefinitions> |
22 | <Grid.ColumnDefinitions> |
23 | <ColumnDefinition Width= "100" /> |
24 | <ColumnDefinition Width= "*" /> |
25 | </Grid.ColumnDefinitions> |
27 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" >用户名:</TextBlock> |
28 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "0" Name= "txtUserName" VerticalAlignment= "Center" Mask= "aaaaaaaaaa" Margin= "0,0,10,0" /> |
29 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "1" >密码:</TextBlock> |
30 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "1" Name= "txtPassWord" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
31 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "2" >重复密码:</TextBlock> |
32 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "2" Name= "txtPassWord2" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
33 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "3" >生日:</TextBlock> |
34 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "3" Name= "txtBirthday" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
35 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "4" >电子邮件:</TextBlock> |
36 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "4" Name= "txtEmail" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
37 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "5" >电话号码:</TextBlock> |
38 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "5" Name= "txtTel" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
39 | <TextBlock VerticalAlignment= "Center" TextAlignment= "Right" Grid.Row= "6" >手机号码:</TextBlock> |
40 | <telerik:RadMaskedTextBox Grid.Column= "1" Grid.Row= "6" Name= "txtMobile" VerticalAlignment= "Center" Margin= "0,0,10,0" /> |
42 | <StackPanel Grid.Row= "8" Grid.Column= "1" Orientation= "Horizontal" Height= "22" > |
43 | <telerik:RadButton Content= "提 交" Width= "70" Name= "btnSubmit" Click= "btnSubmit_Click" /> |
44 | <telerik:RadButton Content= "取 消" Width= "70" Margin= "10,0,0,0" Name= "btnCancel" Click= "btnCancel_Click" /> |
弹出窗口Xaml.cs部分
02 | using System.Collections.Generic; |
04 | using Telerik.Windows.Controls; |
06 | namespace Telerik.Sample |
08 | public partial class PopWinUserReg : RadWindow |
10 | public PopWinUserReg() |
12 | InitializeComponent(); |
13 | this .Loaded += new RoutedEventHandler(PopWinUserReg_Loaded); |
16 | void PopWinUserReg_Loaded( object sender, RoutedEventArgs e) |
18 | Dictionary< string , Object> dicPars = this .Tag as Dictionary< string , object >; |
21 | string id = dicPars[ "id" ].ToString(); |
22 | RadWindow.Alert( "传入参数为:" + id); |
26 | private void btnCancel_Click( object sender, RoutedEventArgs e) |
28 | this .DialogResult = false ; |
32 | private void btnSubmit_Click( object sender, RoutedEventArgs e) |
34 | this .DialogResult = true ; |
35 | this .Tag = "回传给父窗口的值在这里!" ; |
父窗口Xaml.cs部分:
02 | using System.Collections; |
03 | using System.Collections.Generic; |
07 | using System.Windows.Controls; |
08 | using System.Windows.Documents; |
09 | using System.Windows.Input; |
10 | using System.Windows.Media; |
11 | using System.Windows.Media.Animation; |
12 | using System.Windows.Shapes; |
13 | using Telerik.Windows.Controls; |
15 | namespace Telerik.Sample |
17 | public partial class FormInput : UserControl |
19 | PopWinUserReg win= null ; |
20 | Dictionary< string , Object> dicPars = new Dictionary< string , object >(); |
24 | InitializeComponent(); |
26 | this .Loaded += new RoutedEventHandler(FormInput_Loaded); |
27 | this .Unloaded += new RoutedEventHandler(FormInput_Unloaded); |
30 | void FormInput_Loaded( object sender, RoutedEventArgs e) |
32 | win = new PopWinUserReg(); |
33 | win.Loaded += new RoutedEventHandler(win_Loaded); |
34 | win.Closed += new EventHandler<Windows.Controls.WindowClosedEventArgs>(win_Closed); |
37 | void win_Closed( object sender, Windows.Controls.WindowClosedEventArgs e) |
39 | if (!e.DialogResult.HasValue) |
41 | RadWindow.Alert( "直接关闭了弹出窗口!" ); |
43 | else if (e.DialogResult.Value) |
45 | string result = win.Tag.ToString(); |
46 | RadWindow.Alert( "点击“提交”关闭的,传回来的值为:" + result); |
50 | RadWindow.Alert( "点击“取消”关闭的!" ); |
56 | void win_Loaded( object sender, RoutedEventArgs e) |
58 | RadWindow.Alert( "弹出窗口加载完成!" ); |
61 | void FormInput_Unloaded( object sender, RoutedEventArgs e) |
70 | private void btnReg_Click( object sender, RoutedEventArgs e) |
73 | string PARAM_ID = "id" ; |
74 | if (dicPars.ContainsKey(PARAM_ID)) |
76 | dicPars.Remove(PARAM_ID); |
78 | dicPars.Add(PARAM_ID, 1); |