在企业级项目中,子窗口(ChildWindow)是一个常用控件,其展示方式是以弹出窗口来显示信息。 这里我将演示,子窗口传递参数到父窗口的方法。由于我的开发环境都是英文环境,所以部分中文可能显示不正常,请大家见谅。
我们的目的是希望用户在子窗口输入一串文字,然后点击提交后,字符串将被返回显示在父窗口。
1. 首先创建一个新项目 “SLChildWindow",
2. 然后在新项目中,右键点击添加,添加一个新项目,选择“子窗口”(ChildWindow), 改名为"ChildWindowDemo.xaml",添加完成后,在子窗口中添加一个文本框,名为 txtUserInfo,
1<controls:ChildWindow x:Class="SLChildWindow.ChildWindowDemo"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
5 Width="400" Height="300"
6 Title="ChildWindowDemo">
7 <Grid x:Name="LayoutRoot" Margin="2">
8 <Grid.RowDefinitions>
9 <RowDefinition />
10 <RowDefinition Height="Auto" />
11 </Grid.RowDefinitions>
12
13 <TextBox x:Name="txtUserInfor" Grid.Row="0" />
14
15 <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
16 <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
17 </Grid>
18</controls:ChildWindow>
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
5 Width="400" Height="300"
6 Title="ChildWindowDemo">
7 <Grid x:Name="LayoutRoot" Margin="2">
8 <Grid.RowDefinitions>
9 <RowDefinition />
10 <RowDefinition Height="Auto" />
11 </Grid.RowDefinitions>
12
13 <TextBox x:Name="txtUserInfor" Grid.Row="0" />
14
15 <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
16 <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
17 </Grid>
18</controls:ChildWindow>
3. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,
1 string testString;
2
3 public string TestString
4
5 {
6
7 get {return testString;}
8
9 set { testString = value; }
10
11 }
12
2
3 public string TestString
4
5 {
6
7 get {return testString;}
8
9 set { testString = value; }
10
11 }
12
4. 在子窗口中,建立一个EventHandler,父窗口将用该事件控制器获取子窗口参数,
1public event EventHandler OkClicked;
5. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKButton_Click,CancelButton_Click; 在OKButton_Click中调用前面我们定义的时间控制器OkClicked,
1 private void OKButton_Click(object sender, RoutedEventArgs e)
2 {
3 if (OkClicked != null)
4 {
5 TestString = txtUserInfor.Text;
6 OkClicked(this, new EventArgs());
7 }
8 this.DialogResult = true;
9 }
2 {
3 if (OkClicked != null)
4 {
5 TestString = txtUserInfor.Text;
6 OkClicked(this, new EventArgs());
7 }
8 this.DialogResult = true;
9 }
6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,
private ChildWindowDemo childWindowDemo = new ChildWindowDemo();
7. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,
1 public MainPage()
2 {
3 InitializeComponent();
4 childWindowDemo.OkClicked += new EventHandler(childWindowDemo_OkClicked);
5 }
6
7 private void childWindowDemo_OkClicked(object sender, EventArgs e)
8 {
9 tbInfo.Text = childWindowDemo.TestString;
10 }
11
12 private void btPopup_Click(object sender, RoutedEventArgs e)
13 {
14 childWindowDemo.Show();
15 }
16
2 {
3 InitializeComponent();
4 childWindowDemo.OkClicked += new EventHandler(childWindowDemo_OkClicked);
5 }
6
7 private void childWindowDemo_OkClicked(object sender, EventArgs e)
8 {
9 tbInfo.Text = childWindowDemo.TestString;
10 }
11
12 private void btPopup_Click(object sender, RoutedEventArgs e)
13 {
14 childWindowDemo.Show();
15 }
16
8. 最后通过子窗口属性获取子窗口用户输入信息。