加班加班还是加班 = = 真愁人,两个礼拜了基本上没怎么看Silverlight,感觉手都生了大脑都锈了...只好拿出之前的一个小例子写写了
Silverlight中子窗体可以说解决了很多人在C#.Net中弹层(Div)的问题,在C#.Net中,想弹出一个子窗体要不就得用js要不就得用div,然后还要加以美工传值判断...等一系列繁杂的工作,而现在只要新建一个子窗体就可以解决了
今天说我研究过的三种情况:简单弹窗,获得子窗体命令,三级弹窗
1. 简单弹窗:点击按钮弹出子窗体,选择子窗体中的按钮关闭子窗体
2. 获得子窗口命令:关闭子窗口的同时获得子窗口DialogResult的属性
3. 三级弹窗:在弹出子窗口的基础上再弹出一个子窗口
MainPage.xaml
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
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(子窗口)
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
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
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)子窗体才会关闭,否则子窗体将一直停留在浏览器上
其他的我注释写的已经很清楚了,本身子窗体与父窗体的交换就不是难点,只不过之前研究过就写上了...