zoukankan      html  css  js  c++  java
  • Silverlight子窗口(ChildWindow)传递参数到父窗口演示

    在企业级项目中,子窗口(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>

    3. 在子窗口后台代码中创建一个字符串类型的属性,该属性将用来保存用户输入的字符串,

    1 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         }
     6 
     7         private void CancelButton_Click(object sender, RoutedEventArgs e)
     8         {
     9             this.DialogResult = false;
    10         }

    6. 在父窗口MainPage.xaml中建立一个子窗口的实例,方便父窗口调用子窗口,

    1 private ChildWindowDemo childWindowDemo = new ChildWindowDemo();

    7. 在父窗口MainPage.xaml中调用子窗口的OkClicked事件,

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Net;
     5 using System.Windows;
     6 using System.Windows.Controls;
     7 using System.Windows.Documents;
     8 using System.Windows.Input;
     9 using System.Windows.Media;
    10 using System.Windows.Media.Animation;
    11 using System.Windows.Shapes;
    12 
    13 namespace SLChildWindow
    14 {
    15     public partial class MainPage : UserControl
    16     {
    17         private ChildWindowDemo childWindowDemo = new ChildWindowDemo();
    18 
    19         public MainPage()
    20         {
    21             InitializeComponent();
    22             childWindowDemo.Closed += new EventHandler(childWindowDemo_Closed);
    23         }
    24 
    25         private void childWindowDemo_Closed(object sender, EventArgs e)
    26         {
    27             bool? result = childWindowDemo.DialogResult;
    28 
    29             if (result.HasValue && result.Value)
    30             {
    31                 tbInfo.Text = childWindowDemo.TestString;
    32             }
    33         }
    34 
    35         //private void childWindowDemo_OkClicked(object sender, EventArgs e)
    36         //{
    37         //    tbInfo.Text = childWindowDemo.TestString;
    38         //}
    39 
    40         private void btPopup_Click(object sender, RoutedEventArgs e)
    41         {
    42             childWindowDemo.Show();
    43         }
    44 
    45 
    46 
    47 
    48     }
    49 }

     文章来源:http://silverlightchina.net/html/tips/2009/1125/261.html

  • 相关阅读:
    为什么构造函数不能声明为虚函数,析构函数可以
    _stdcall,_cdecl区别
    C++的dllexport和dllimport
    什么样的代码才是好代码
    苦逼三流小公司程序员这半年找工作经历(3)——选择offer
    c# DataTable、DataSet、DataReader
    C# 定时器
    sql 中如何将返回的记录某一条置顶
    c# devExpress控件 comboBoxEdit,gridControl1,labelcontrol、xtraReports ,ButtonEdit,dateEdit
    c# 公共方法
  • 原文地址:https://www.cnblogs.com/qiernonstop/p/3735799.html
Copyright © 2011-2022 走看看