WPF中的ChildWindow----WPF的toolkit使用
Silverlight中有ChildWindow这个东西,上午想起来WPF中是否也有这个东西呢?
其实如果真用代码也是可以实现ChildWindow,可是有没有更便捷的方法呢?我想到了WPF的toolkit,发现新的版本中还真是有这么个控件。首先声明,这里是个控件,这个Silverlight里面的ChildWindow的实现方式不一样,Silverlight中如何实现不再赘述。因为silverlight中的childwindow的固定思维使对这个东西的使用走了弯路,迷惑了好一会,所以分享给大家。
首先需要下载wpftoolkit和Extended WPF Toolkit Binaries。然后在项目中添加对WPFToolkit和WPFToolkit.Extended的引用:
在wpf窗口的<Window 节点中添加对命名空间的引用,加入一行
xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
<extToolkit:ChildWindow x:Name="childWindow" Visibility="Hidden" Caption="My Child Window" Width="456" Closed="childWindow_Closed" IsModal="True" Height="253">
<Grid Margin="10" >
<StackPanel Orientation="Horizontal" Margin="8,8,8,0">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF4F8FF" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<Button Width="65" Margin="100,76.5,2,0" x:Name="okbtn" Click="okbtn_Click" Content="获取值" Height="40" RenderTransformOrigin="-0.354,0.525" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
<Button Width="65" Margin="50,76.5,2,0" x:Name="cancalbtn" Click="cancalbtn_Click" Content="设置值" Height="40" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
</StackPanel>
</Grid>
</extToolkit:ChildWindow>
private void OpenChildWindow_Click(object sender, RoutedEventArgs e)
{
childWindow.Visibility = Visibility.Visible;
childWindow.Show();
}
现将截图如下:
运行
打开childwindow:
获取label的值:
设置label的值:
子窗口关闭事件:
页面完整代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="blogtest.MainWindow"
Title="MainWindow" Height="480" Width="640">
<Grid>
<Button Content="打开子窗口" Height="40" HorizontalAlignment="Left" Margin="56,75,0,0" x:Name="OpenChildWindow" Click="OpenChildWindow_Click" VerticalAlignment="Top" Width="100" />
<Label Height="28" HorizontalAlignment="Left" Margin="56,12,0,0" x:Name="label1" VerticalAlignment="Top" Width="273" Content="初始值100" />
<extToolkit:ChildWindow x:Name="childWindow" Visibility="Hidden" Caption="My Child Window" Width="456" Closed="childWindow_Closed" IsModal="True" Height="253">
<Grid Margin="10" >
<StackPanel Orientation="Horizontal" Margin="8,8,8,0">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF4F8FF" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
<Button Width="65" Margin="100,76.5,2,0" x:Name="okbtn" Click="okbtn_Click" Content="获取值" Height="40" RenderTransformOrigin="-0.354,0.525" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
<Button Width="65" Margin="50,76.5,2,0" x:Name="cancalbtn" Click="cancalbtn_Click" Content="设置值" Height="40" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
</StackPanel>
</Grid>
</extToolkit:ChildWindow>
</Grid>
</Window>
后台程序完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
namespace blogtest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OpenChildWindow_Click(object sender, RoutedEventArgs e)
{
childWindow.Visibility = Visibility.Visible;
childWindow.Show();
}
private void okbtn_Click(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show("获取的到的值为:" + label1.Content);
}
private void cancalbtn_Click(object sender, RoutedEventArgs e)
{
label1.Content = "值被设置为50";
}
private void childWindow_Closed(object sender, EventArgs e)
{
System.Windows.MessageBox.Show("ChildWindow关闭了");
}
}
}
欢迎广大园友共同探讨,本人技术水平有限,如有不足之处,还请园友多多批评指正,谢谢。