Silverlight中的childwindow

举例说明了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;
        }

 

 

posted @ 2011-07-17 14:36  chenping2008  阅读(3184)  评论(0编辑  收藏  举报