Silverlight4.0(8) 之 父窗体及子窗体变换

  加班加班还是加班 = =  真愁人,两个礼拜了基本上没怎么看Silverlight,感觉手都生了大脑都锈了...只好拿出之前的一个小例子写写了

  Silverlight中子窗体可以说解决了很多人在C#.Net中弹层(Div)的问题,在C#.Net中,想弹出一个子窗体要不就得用js要不就得用div,然后还要加以美工传值判断...等一系列繁杂的工作,而现在只要新建一个子窗体就可以解决了
  今天说我研究过的三种情况:简单弹窗,获得子窗体命令,三级弹窗

  1. 简单弹窗:点击按钮弹出子窗体,选择子窗体中的按钮关闭子窗体

  2. 获得子窗口命令:关闭子窗口的同时获得子窗口DialogResult的属性

  3. 三级弹窗:在弹出子窗口的基础上再弹出一个子窗口

  MainPage.xaml

ContractedBlock.gif ExpandedBlockStart.gif MainPage.xaml
 
   
< Grid x:Name = " LayoutRoot " Background = " White " >
< HyperlinkButton Content = " 简单弹窗 " Height = " 23 " FontSize = " 16 " HorizontalAlignment = " Left " Margin = " 81,311,0,0 " Name = " hyperlinkButton0 " VerticalAlignment = " Top " Width = " 100 " Click = " hyperlinkButton0_Click " />
< HyperlinkButton Content = " 三级弹窗 " Height = " 23 " FontSize = " 16 " HorizontalAlignment = " Left " Margin = " 462,311,0,0 " Name = " hyperlinkButton2 " VerticalAlignment = " Top " Width = " 100 " Click = " hyperlinkButton2_Click " />
< HyperlinkButton Content = " 获得子窗体命令 " Height = " 23 " FontSize = " 16 " HorizontalAlignment = " Left " Margin = " 267,311,0,0 " Name = " hyperlinkButton1 " VerticalAlignment = " Top " Width = " 100 " Click = " hyperlinkButton1_Click " />
</ Grid >

  MainPage.xaml.cs

ContractedBlock.gif ExpandedBlockStart.gif MainPage.xaml.cs
 
   
// 弹出ChildWindow1子窗口
private void hyperlinkButton0_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c1
= new ChildWindow1(); // 实例化子窗口
c1.Show(); // 打开子窗口
}
// 弹出ChildWindow1子窗口并触发子窗口关闭事件
private void hyperlinkButton1_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c2
= new ChildWindow1(); // 实例化子窗口
c2.Show(); // 打开子窗口
c2.Closed += new EventHandler(c2_Closed); // 委托子窗口关闭事件
}
// 子窗口关闭事件
void c2_Closed( object sender, EventArgs e)
{
ChildWindow1 c2
= (ChildWindow1)sender; // 子窗口获得按钮事件
string state = c2.DialogResult.ToString(); // 获得窗口关闭时DialogResult的属性
MessageBox.Show(state); // 输出该属性的值
}
// 子窗口弹出事件
private void hyperlinkButton2_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c3
= new ChildWindow1(); // 实例化子窗口
c3.Show(); // 打开子窗口
}

  ChildWindow1.xaml(子窗口)

 

ContractedBlock.gif ExpandedBlockStart.gif ChildWindow1.xaml
 
   
< Grid x:Name = " LayoutRoot " Margin = " 2 " >
< Grid.RowDefinitions >
< RowDefinition />
< RowDefinition Height = " Auto " />
</ Grid.RowDefinitions >

< Button x:Name = " CancelButton " Content = " 取消 " Click = " CancelButton_Click " Width = " 75 " Height = " 23 " HorizontalAlignment = " Right " Margin = " 0,12,0,0 " Grid.Row = " 1 " />
< Button x:Name = " OKButton " Content = " 关闭窗口 " Click = " OKButton_Click " Width = " 75 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 0,12,0,0 " Grid.Row = " 1 " />
< Button Content = " 返回窗口状态 " Height = " 23 " HorizontalAlignment = " Right " Margin = " 0,12,222,0 " Name = " button1 " Width = " 75 " Grid.Row = " 1 " Click = " button1_Click " />
< Button Content = " 三级弹窗 " Height = " 23 " HorizontalAlignment = " Right " Margin = " 0,12,141,0 " Name = " button2 " Width = " 75 " Grid.Row = " 1 " Click = " button2_Click " />
</ Grid >

 

 

  ChildWindow1.xaml.cs

ContractedBlock.gif ExpandedBlockStart.gif ChildWindow1.xaml.cs
 
   
// 弹出ChildWindow1子窗口
private void hyperlinkButton0_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c1
= new ChildWindow1(); // 实例化子窗口
c1.Show(); // 打开子窗口
}
// 弹出ChildWindow1子窗口并触发子窗口关闭事件
private void hyperlinkButton1_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c2
= new ChildWindow1(); // 实例化子窗口
c2.Show(); // 打开子窗口
c2.Closed += new EventHandler(c2_Closed); // 委托子窗口关闭事件
}
// 子窗口关闭事件
void c2_Closed( object sender, EventArgs e)
{
ChildWindow1 c2
= (ChildWindow1)sender; // 子窗口获得按钮事件
string state = c2.DialogResult.ToString(); // 获得窗口关闭时DialogResult的属性
MessageBox.Show(state); // 输出该属性的值
}
// 子窗口弹出事件
private void hyperlinkButton2_Click( object sender, RoutedEventArgs e)
{
ChildWindow1 c3
= new ChildWindow1(); // 实例化子窗口
c3.Show(); // 打开子窗口
}

 ChildWindow2.xaml

ContractedBlock.gif ExpandedBlockStart.gif ChildWindow2.xaml
 
   
< Grid x:Name = " LayoutRoot " Margin = " 2 " >
< Grid.RowDefinitions >
< RowDefinition />
< RowDefinition Height = " Auto " />
</ Grid.RowDefinitions >

< 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 " />
</ Grid >

 

 

 这里说一下子窗体的DialogResult属性

DialogResult属性在子窗体打开时其值是null,当DialogResult属性值发生改变时(true/false)子窗体才会关闭,否则子窗体将一直停留在浏览器上

其他的我注释写的已经很清楚了,本身子窗体与父窗体的交换就不是难点,只不过之前研究过就写上了...

 

 

转载于:https://www.cnblogs.com/terryaspx/archive/2010/06/27/1766203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值