几乎所有的业务系统都有弹出窗口,典型场景有二种 :
1、简单的弹出一个对话框显示信息,比如下面这样:
这个很简单,代码示例如下:
代码语言:javascript复制DialogParameters pars = new DialogParameters();
pars.Header = "信息";
pars.Content = "Hello World";
RadWindow.Alert(pars);
2、点击某条记录的“编辑”按钮,传入ID参数,弹出一个窗口,编辑保存后,将操作结果返回给父窗口
这种场景下,要求:
a)弹出窗口能接受到父窗口传过来的参数
b)弹出窗口关闭时,父窗口要能区分出是通过什么操作关闭的(比如:是直接点击右上角的X按钮关的,还是点击“提交”按钮关的,或是点击“取消”按钮关的)
c)弹出窗关闭后,父窗口要能知道操作结果
示例代码如下:
弹出窗口Xaml部分:
代码语言:javascript复制<telerik:RadWindow x:Class="Telerik.Sample.PopWinUserReg"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="640" WindowStartupLocation="CenterScreen" Header="会员注册" Padding="10" Width="640" Height="300" ResizeMode="NoResize">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="Auto" MinHeight="10" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" TextAlignment="Right">用户名:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="0" Name="txtUserName" VerticalAlignment="Center" Mask="aaaaaaaaaa" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="1">密码:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="1" Name="txtPassWord" VerticalAlignment="Center" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="2">重复密码:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="2" Name="txtPassWord2" VerticalAlignment="Center" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="3">生日:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="3" Name="txtBirthday" VerticalAlignment="Center" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="4">电子邮件:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="4" Name="txtEmail" VerticalAlignment="Center" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="5">电话号码:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="5" Name="txtTel" VerticalAlignment="Center" Margin="0,0,10,0" />
<TextBlock VerticalAlignment="Center" TextAlignment="Right" Grid.Row="6">手机号码:</TextBlock>
<telerik:RadMaskedTextBox Grid.Column="1" Grid.Row="6" Name="txtMobile" VerticalAlignment="Center" Margin="0,0,10,0" />
<StackPanel Grid.Row="8" Grid.Column="1" Orientation="Horizontal" Height="22">
<telerik:RadButton Content="提 交" Width="70" Name="btnSubmit" Click="btnSubmit_Click" />
<telerik:RadButton Content="取 消" Width="70" Margin="10,0,0,0" Name="btnCancel" Click="btnCancel_Click" />
</StackPanel>
</Grid>
</telerik:RadWindow>
弹出窗口Xaml.cs部分
代码语言:javascript复制using System;
using System.Collections.Generic;
using System.Windows;
using Telerik.Windows.Controls;
namespace Telerik.Sample
{
public partial class PopWinUserReg : RadWindow
{
public PopWinUserReg()
{
InitializeComponent();
this.Loaded = new RoutedEventHandler(PopWinUserReg_Loaded);
}
void PopWinUserReg_Loaded(object sender, RoutedEventArgs e)
{
Dictionary<string, Object> dicPars = this.Tag as Dictionary<string, object>;
if (dicPars != null)
{
string id = dicPars["id"].ToString();
RadWindow.Alert("传入参数为:" id);
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Tag = "回传给父窗口的值在这里!";
this.Close();
}
}
}
父窗口Xaml.cs部分:
代码语言:javascript复制using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
namespace Telerik.Sample
{
public partial class FormInput : UserControl
{
PopWinUserReg win=null;
Dictionary<string, Object> dicPars = new Dictionary<string, object>();
public FormInput()
{
InitializeComponent();
this.Loaded = new RoutedEventHandler(FormInput_Loaded);
this.Unloaded = new RoutedEventHandler(FormInput_Unloaded);
}
void FormInput_Loaded(object sender, RoutedEventArgs e)
{
win = new PopWinUserReg();
win.Loaded = new RoutedEventHandler(win_Loaded);
win.Closed = new EventHandler<Windows.Controls.WindowClosedEventArgs>(win_Closed);
}
void win_Closed(object sender, Windows.Controls.WindowClosedEventArgs e)
{
if (!e.DialogResult.HasValue)
{
RadWindow.Alert("直接关闭了弹出窗口!");
}
else if (e.DialogResult.Value)
{
string result = win.Tag.ToString();
RadWindow.Alert("点击“提交”关闭的,传回来的值为:" result);
}
else
{
RadWindow.Alert("点击“取消”关闭的!");
}
}
void win_Loaded(object sender, RoutedEventArgs e)
{
RadWindow.Alert("弹出窗口加载完成!");
}
void FormInput_Unloaded(object sender, RoutedEventArgs e)
{
if (win != null)
{
win.Close();
win = null;
}
}
private void btnReg_Click(object sender, RoutedEventArgs e)
{
#region 传参数到弹出窗口的Tag
string PARAM_ID = "id";
if (dicPars.ContainsKey(PARAM_ID))
{
dicPars.Remove(PARAM_ID);
}
dicPars.Add(PARAM_ID, 1);
win.Tag = dicPars;
#endregion
win.ShowDialog();
}
}
}