舉例說明了silverlight中childwindow的用法:
AddPerson.xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="30,22,0,0" Name="btnAddPerson" VerticalAlignment="Top" Width="75" Click="btnAddPerson_Click" /> <TextBlock Height="80" HorizontalAlignment="Left" Margin="30,62,0,0" Name="tbPersonInfo" Text="" VerticalAlignment="Top" Width="347" /> </Grid>



AddPerson中的cs代碼:
private void btnAddPerson_Click(object sender, RoutedEventArgs e)
{
ChildWindow1 child = new ChildWindow1();
child.Closed += new EventHandler(child_Closed);
child.Show();
}

 

void child_Closed(object sender, EventArgs e)
{
ChildWindow1 child=sender as ChildWindow1;
String personInfo = String.Empty;
personInfo = String.Format("The Person who you input Name:{0},Age:{1}", (child.FindName("txtName") as TextBox).Text, (child.FindName("txtAge") as TextBox).Text);
//MessageBox.Show(personInfo);
tbPersonInfo.Text = personInfo;
}
其中child.Close定義了childwindow關閉後所做的事件。
添加一個childwindow:
<controls:ChildWindow x:Class="SilverlightApplication2.ChildWindow1"
xmlns="HTTP://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="HTTP://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="400" Height="300"
Title="請輸入資訊">
<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="Right" Margin="0,12,79,0" Grid.Row="1" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="58,66,0,0" Name="tbName" Text="Name:" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="70,107,0,0" Name="tbAge" Text="Age:" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,62,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,107,0,0" Name="txtAge" VerticalAlignment="Top" Width="120" />
</Grid>
</controls:ChildWindow>
childwindow中的cs代碼:
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}

 

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()