Silverlight子窗口(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. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,
string testString;
public string TestString
{
get {return testString;}
set { testString = value; }
}
4. 在子窗口中,建立一个EventHandler,父窗口将用该事件控制器获取子窗口参数,
public event EventHandler OkClicked;
5. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKButton_Click,CancelButton_Click; 在OKButton_Click中调用前面我们定义的时间控制器OkClicked,
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (OkClicked != null)
{
TestString = txtUserInfor.Text;
OkClicked(this, new EventArgs());
}
this.DialogResult = true;
}
{
if (OkClicked != null)
{
TestString = txtUserInfor.Text;
OkClicked(this, new EventArgs());
}
this.DialogResult = true;
}
6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,
private ChildWindowDemo childWindowDemo = new ChildWindowDemo();
7. 在父窗口MainPage.xaml页面
代码
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="tbInfo" Grid.Row="0"/>
<Button x:Name="btPopup" Grid.Row="1" Click="btPopup_Click" Width="140" Height="100" Content="打开子窗口"/>
</Grid>
8. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,
代码
public MainPage()
{
InitializeComponent();
childWindowDemo.OkClicked += new EventHandler(childWindowDemo_OkClicked);
}
private void childWindowDemo_OkClicked(object sender, EventArgs e)
{
tbInfo.Text = childWindowDemo.TestString;
}
private void btPopup_Click(object sender, RoutedEventArgs e)
{
childWindowDemo.Show();
}