有关Silverlight ChildWindow组件(子窗体组件)的研究——Silverlight学习笔记(2)
说明:通过学习子窗体的各项特性,您将了解到子窗体的基本使用方法。通过学习实例,应当重点掌握子窗体与父窗体的数据通讯。
组件所在命名空间:
System.Windows.Controls
组件常用属性:
DialogResult:获取或者设置一个值用来显示子窗体的反馈内容是否被接受或是取消。
HasCloseButton:获取或者设置一个值用来显示子窗体是否包含关闭按钮。
OverlayBrush:获取或者设置被用于当子窗体打开时覆盖在父窗体上的遮盖层的笔刷。
OverlayOpacity:获取或者设置被用于当子窗体打开时覆盖在父窗体上的遮盖层的笔刷的透明度。
Title:获取或者设置子窗体的窗口标题。
组件常用事件:
Closed:当子窗体关闭后发生。
Closing:当子窗体正在关闭时发生。
组件常用方法:
Close:关闭子窗体。
Show:打开子窗体并返回而不等待该子窗体关闭
实例:
效果如下:
代码段:
MainPage.xaml代码:(由Microsoft Expression Blend 3设计)
<UserControl
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"
mc:Ignorable="d" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="ChildWindow.MainPage"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="186"/>
<ColumnDefinition Width="415"/>
<ColumnDefinition Width="39"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="87"/>
<RowDefinition Height="220"/>
<RowDefinition Height="173"/>
</Grid.RowDefinitions>
<Button x:Name="btnOpenChildwindow" FontSize="16" Height="29" Margin="29,0,179,40" VerticalAlignment="Bottom" Content="打开子窗体" Width="207" Grid.Row="2" Grid.Column="1"/>
<CheckBox x:Name="cbHasCloseButton" FontSize="16" Margin="29,69,179,73" Content="是否包含关闭按钮" Width="207" Grid.Row="2" Grid.Column="1"/>
<TextBox x:Name="tbChilewindowTitle" Height="24" Margin="29,41,179,0" VerticalAlignment="Top" TextWrapping="Wrap" Width="207" Grid.Row="2" Grid.Column="1"/>
<dataInput:Label FontSize="16" Margin="29,9,179,0" Content="子窗体标题:" Height="28" VerticalAlignment="Top" Grid.Row="2" Grid.Column="1"/>
<Rectangle x:Name="rtBox" Fill="White" Stroke="Black" Margin="8,60,28,10" Height="150" Grid.Row="1"/>
<dataInput:Label Margin="11,24,25,30" Content="子窗体返回结果:" FontSize="16"/>
<TextBlock x:Name="tbResult" Margin="29,60,26,10" Text="<None>" TextWrapping="Wrap" Foreground="Black" FontSize="14" Width="360" Height="150" Grid.Row="1" Grid.Column="1" />
<dataInput:Label Height="25" Margin="29,19,0,0" VerticalAlignment="Top" Content="2.选中的雇员信息:" FontSize="16" Width="178" Grid.Row="1" HorizontalAlignment="Left" Grid.Column="1" d:LayoutOverrides="Width"/>
<dataInput:Label Height="26" Margin="11,19,28,0" VerticalAlignment="Top" Content="1.矩形框背景:" FontSize="16" Grid.Row="1"/>
</Grid>
</UserControl>
MainPage.xaml.cs代码:
using System;
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 ChildWindow.controls;//引入设计的子窗体所在的命名空间
namespace ChildWindow
{
public partial class MainPage : UserControl
{
//定义子窗口
ChildWindowDemo cw = new ChildWindowDemo();
public MainPage()
{
InitializeComponent();
//注册事件触发处理
this.btnOpenChildwindow.Click += new RoutedEventHandler(btnOpenChildwindow_Click);
this.cw.Closed += new EventHandler(cw_Closed);
}
//子窗体关闭事件处理
void cw_Closed(object sender, EventArgs e)
{
if (cw.DialogResult == true)
{
try
{
tbResult.Text = "雇员号:" + Convert.ToString(cw.returnEM.EmployeeID) + ""n雇员姓名:" + cw.returnEM.EmployeeName + ""n雇员年龄:" + Convert.ToString(cw.returnEM.EmployeeAge);
SolidColorBrush scb = new SolidColorBrush();
switch (cw.returnColor)
{
case "Red":
scb.Color = Colors.Red;
break;
case "Blue":
scb.Color = Colors.Blue;
break;
case "Yellow":
scb.Color = Colors.Yellow;
break;
case "Green":
scb.Color = Colors.Green;
break;
case "Black":
scb.Color = Colors.Black;
break;
}
rtBox.Fill = scb;
}
catch
{ }
}
}
void btnOpenChildwindow_Click(object sender, RoutedEventArgs e)
{
if (tbChilewindowTitle.Text != String.Empty)
{
cw.Title = tbChilewindowTitle.Text;//设置子窗体标题
}
if (cbHasCloseButton.IsChecked == true)//设置子窗体是否含有关闭按钮
{
cw.HasCloseButton = true;
}
else
{
cw.HasCloseButton = false;
}
cw.Show();//显示子窗体
}
}
}
ChildWindowDemo.xaml代码:
<controls:ChildWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" x:Class="ChildWindow.controls.ChildWindowDemo"
Width="400" Height="300"
Title="ChildWindowDemo">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<data:DataGrid x:Name="dgEmployee" Margin="17,8,20,69" HorizontalGridLinesBrush="#FF4F4FE6" VerticalGridLinesBrush="#FF4F4FE6" RowBackground="#AA4494E4"/>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
<dataInput:Label Height="29" HorizontalAlignment="Left" Margin="18,0,0,22" VerticalAlignment="Bottom" Width="97" FontSize="16" Content="矩形填充色:"/>
<ComboBox x:Name="cbColor" Height="27" Margin="119,0,20,22" VerticalAlignment="Bottom" SelectedIndex="0">
<ComboBoxItem Content="Red"/>
<ComboBoxItem Content="Blue"/>
<ComboBoxItem Content="Yellow"/>
<ComboBoxItem Content="Green"/>
<ComboBoxItem Content="Black"/>
</ComboBox>
</Grid>
</controls:ChildWindow>
ChildWindowDemo.xaml.cs代码:
using System;
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;
namespace ChildWindow.controls
{
//定义数据类
public class Employees
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int EmployeeAge { get; set; }
public Employees()
{ }
public Employees(int employeeid, string employeename, int employeeage)
{
EmployeeID = employeeid;
EmployeeName = employeename;
EmployeeAge = employeeage;
}
}
public partial class ChildWindowDemo : System.Windows.Controls.ChildWindow
{
//全局变量声明
private List<Employees> em = new List<Employees>();
public Employees returnEM = new Employees();//传回父窗体的变量1
public string returnColor;//传回父窗体的变量2
public ChildWindowDemo()
{
InitializeComponent();
this.dgEmployee.HeadersVisibility = DataGridHeadersVisibility.All;//同时显示DataGrid的行头与列头
//注册事件触发处理
this.Loaded += new RoutedEventHandler(ChildWindowDemo_Loaded);
this.dgEmployee.CurrentCellChanged += new EventHandler<EventArgs>(dgEmployee_CurrentCellChanged);
this.cbColor.SelectionChanged += new SelectionChangedEventHandler(cbColor_SelectionChanged);
//设置ChildWindow覆盖父窗体的遮盖层的笔刷及其透明度
this.OverlayBrush = new SolidColorBrush(Colors.Cyan);
this.OverlayOpacity = 0.5;
}
void cbColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cbColor.SelectedItem != null)
{
returnColor = ((ComboBoxItem)cbColor.SelectedItem).Content.ToString();
}
}
void dgEmployee_CurrentCellChanged(object sender, EventArgs e)
{
returnEM = (Employees)dgEmployee.SelectedItem;
}
void ChildWindowDemo_Loaded(object sender, RoutedEventArgs e)
{
em.Clear();
em.Add(new Employees(1, "张三", 23));
em.Add(new Employees(2, "李四", 24));
em.Add(new Employees(3, "王五", 25));
dgEmployee.ItemsSource = em;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
}