• silverlight 对ChildWindow返回给父窗体值的理解(转载)


    这篇文章是我对ChildWindow的理解,举例说明:

    有时候在项目中需要弹出子窗体进行一些操作,然后将操作的值返回到父窗体中。

    下图是子窗体的界面(比较粗糙。。。。)

    下面贴出其代码:

    子窗体前台代码
     <controls:ChildWindow x:Class="FineMmarketing.Controls.SelectChild"
                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="365" Height="226"
                Title="请选择纬度">
         <Grid x:Name="LayoutRoot" Margin="2" >
             <Grid.RowDefinitions>
                 <RowDefinition />
                 <RowDefinition Height="Auto" />
             </Grid.RowDefinitions>
             <StackPanel Margin="0,0,0,37">           
                 <TextBlock Width="100" Height="30" Text="请输入纬度:" Margin="83 35 0 0" HorizontalAlignment="Left" />
                 <TextBox x:Name="Txt" Width="200" Height="30" HorizontalAlignment="Center" Margin="24 0 0 0"/>
             </StackPanel>
            
             <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" />
         </Grid>
     </controls:ChildWindow>

    子窗体后台代码
      /// <summary>
             /// 确定按钮
             /// </summary>
             /// <param name="sender"></param>
             /// <param name="e"></param>
             private void OKButton_Click(object sender, RoutedEventArgs e)
             {           
                 if (!String.IsNullOrEmpty(this.Txt.Text))
                 {
                     Regex reg = new Regex("^[0-9]+$");
                     Match ma = reg.Match(this.Txt.Text.ToString());
                     if (ma.Success)
                     {
                         Num = Convert.ToInt32(this.Txt.Text.ToString());
                         if (Num > 10)
                         {
                             MessageBox.Show("纬度不能超过10!");
                             return;
                         }
                         else
                         {
                             this.Tag = Num;
                         }          
                     }
                     else
                     {
                         MessageBox.Show("请输入正确的纬度!");
                     }
                 }
                 else
                 {
                     MessageBox.Show("请输入纬度!");
                 }
                 this.DialogResult = true;
             }
      /// <summary>
             /// 取消按钮
             /// </summary>
             /// <param name="sender"></param>
             /// <param name="e"></param>
             private void CancelButton_Click(object sender, RoutedEventArgs e)
             {
                 this.DialogResult = false;
             }

    然后看一下在父窗体中如何调用子窗体,然后后台定义事件执行子窗体关闭事件。

    子窗体后台代码
     SelectChild myChild = new SelectChild();//申明子窗体对象
     ////定义子窗体关闭事件
     myChild.Closed += new EventHandler(win_Closed);
     
     
      /// <summary>
             /// 子窗口关闭返回值事件
             /// </summary>
             /// <param name="sender"></param>
             /// <param name="e"></param>
             void win_Closed(object sender,EventArgs e)
             {
                 bool? result = myChild.DialogResult;
                 //判断是否是顶级确定按钮关闭并且有返回值
                 if (result.HasValue && result.Value)
                 {
                     //是定义在子窗体中的公有的全局变量
                     MessageBox.Show(myChild.txt);
                 }
             }

  • 相关阅读:
    沙盒解决方案与场解决方案之间的差异
    Windows 7:77 个 Windows 7 提示
    SharePoint disable loopback check
    SharePoint 2010 工作流解决方案:序言
    SharePoint 2010 查看“运行时错误”
    sharepoint 链接库链接在新窗口打开
    如何启用SharePoint 2010的代码块
    沙盒解决方案注意事项
    ie8.0 不能用document.all兼容IE7模式
    php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  • 原文地址:https://www.cnblogs.com/sandea/p/3289874.html
Copyright © 2020-2023  润新知