在企业级项目中,子窗口(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. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,
代码
public string TestString{ get; set; }
4. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKButton_Click,CancelButton_Click; 在OKButton_Click中将用户输入信息赋值给TestString属性,
代码
1 private void OKButton_Click(object sender, RoutedEventArgs e)
2 {
3 TestString = txtUserInfor.Text;
4 this.DialogResult = true;
5 }
2 {
3 TestString = txtUserInfor.Text;
4 this.DialogResult = true;
5 }
6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,
private ChildWindowDemo childWindowDemo = new ChildWindowDemo();
7. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,
代码
1 public MainPage()
2 {
3 InitializeComponent();
4 childWindowDemo.Closed += new EventHandler(childWindowDemo_Closed);
5 }
6
7 private void childWindowDemo_Closed(object sender, EventArgs e)
8 {
9 bool? result = childWindowDemo.DialogResult;
10
11 if (result.HasValue && result.Value)
12 {
13 tbInfo.Text = childWindowDemo.TestString;
14 }
15 }
16
17 private void childWindowDemo_OkClicked(object sender, EventArgs e)
18 {
19 tbInfo.Text = childWindowDemo.TestString;
20 }
21
22 private void btPopup_Click(object sender, RoutedEventArgs e)
23 {
24 childWindowDemo.Show();
2 {
3 InitializeComponent();
4 childWindowDemo.Closed += new EventHandler(childWindowDemo_Closed);
5 }
6
7 private void childWindowDemo_Closed(object sender, EventArgs e)
8 {
9 bool? result = childWindowDemo.DialogResult;
10
11 if (result.HasValue && result.Value)
12 {
13 tbInfo.Text = childWindowDemo.TestString;
14 }
15 }
16
17 private void childWindowDemo_OkClicked(object sender, EventArgs e)
18 {
19 tbInfo.Text = childWindowDemo.TestString;
20 }
21
22 private void btPopup_Click(object sender, RoutedEventArgs e)
23 {
24 childWindowDemo.Show();
25 }
8. 最后通过子窗口属性获取子窗口用户输入信息。