在企业级项目中,子窗口(ChildWindow)是一个常用控件,其展示方式是以弹出窗口来显示信息。 这里我将演示,子窗口传递参数到父窗口的方法。由于我的开发环境都是英文环境,所以部分中文可能显示不正常,请大家见谅。

我们的目的是希望用户在子窗口输入一串文字,然后点击提交后,字符串将被返回显示在父窗口。

1. 首先创建一个新项目 “SLChildWindow",

2. 然后在新项目中,右键点击添加,添加一个新项目,选择“子窗口”(ChildWindow), 改名为"ChildWindowDemo.xaml",添加完成后,在子窗口中添加一个文本框,名为 txtUserInfo,

 <controls:ChildWindow x:Class="SLChildWindow.ChildWindowDemo"
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="ChildWindowDemo">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> <TextBox x:Name="txtUserInfor" Grid.Row="0" /> <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
</Grid>
</controls:ChildWindow>

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

 public string TestString{ get; set; }

4. 当前,在子窗口有两个按钮,一个是Ok按钮,一个是Cancel按钮,后台有两个按钮事件OKButton_Click,CancelButton_Click; 在OKButton_Click中将用户输入信息赋值给TestString属性,

         private void OKButton_Click(object sender, RoutedEventArgs e)
{
TestString = txtUserInfor.Text;
this.DialogResult = true;
} private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}

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

 private ChildWindowDemo childWindowDemo = new ChildWindowDemo();

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace SLChildWindow
{
public partial class MainPage : UserControl
{
private ChildWindowDemo childWindowDemo = new ChildWindowDemo(); public MainPage()
{
InitializeComponent();
childWindowDemo.Closed += new EventHandler(childWindowDemo_Closed);
} private void childWindowDemo_Closed(object sender, EventArgs e)
{
bool? result = childWindowDemo.DialogResult; if (result.HasValue && result.Value)
{
tbInfo.Text = childWindowDemo.TestString;
}
} //private void childWindowDemo_OkClicked(object sender, EventArgs e)
//{
// tbInfo.Text = childWindowDemo.TestString;
//} private void btPopup_Click(object sender, RoutedEventArgs e)
{
childWindowDemo.Show();
} }
}

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

Silverlight子窗口(ChildWindow)传递参数到父窗口演示的更多相关文章

  1. silverlight子窗体操作数据库后刷新父窗体

    silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...

  2. js实现非模态窗口增加数据后刷新父窗口数据

    父窗口是由两个部分组成,一个html的table,一部分是extjs的gird. 点击grid面板[增加]按钮将会弹出非模态窗口进行新数据的编辑页面 下面是按钮的触发函数代码: var a = win ...

  3. 使用 SetParent 制作父子窗口的时候,如何设置子窗口的窗口样式以避免抢走父窗口的焦点

    原文:使用 SetParent 制作父子窗口的时候,如何设置子窗口的窗口样式以避免抢走父窗口的焦点 制作传统 Win32 程序以及 Windows Forms 程序的时候,一个用户看起来独立的窗口本就 ...

  4. iframe窗口嵌套,子窗口跳转重叠在父窗口的问题

    window.top //最顶层窗口 window.self //当前窗口 window.parent //父级窗口 "window.location.href"."lo ...

  5. pb中打开窗口并传递参数

    try long ll_result; ll_result=1;openwithparm(w_sb_order,UserCode);catch(RuntimeError er)  errorMsg=e ...

  6. layui中从子窗口传递数据到父窗口,第三个子弹层的值传给第二个弹层

    最近做一个项目的需要多个弹层,每个弹层中还需要数据传递, 经过测试,以下方法三个弹层才有效,如果只是有两个弹层,请用其它方法 大概如图,看图自己应该明白 如何在在b页面选择好的值传给a页面的问题,这个 ...

  7. mini.open打开窗口时传递参数

    mini.open({ url: "xxx.html", showMaxButton: false, allowResize: false, title: '标题', width: ...

  8. 在引用的laravel的@include子模板中传递参数

    调用传参: @include("message",['msg'=>'中国']) 在message子模板中调用msg的值: {{msg}}

  9. JS子父窗口互相取值赋值详解介绍

    子窗口赋值到父窗口 代码如下 复制代码 <script>function openWin(str) {    window.open(siteurl+"popup/"+ ...

随机推荐

  1. linux命令学习之:ifconfig

    ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在.要想将上述的配置信息永远的存的电脑里,那就要修改网卡 ...

  2. 国内淘宝镜像 cnpm转npm

    npm install -g cnpm --registry=http://registry.npm.taobao.org

  3. js原生语法实现表格操作

    HTML页面: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  4. devexpress WinForms MVVM

    WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...

  5. PAT 1066 图像过滤(15)(代码)

    1066 图像过滤(15 分) 图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: ...

  6. 探索未知种族之osg类生物---器官初始化三

    当判断到viewer中没有一个graphicContext可用时,osg就会默认的进行一次对viewer的实现操作,这样可以保证osg以后可以安心的在屏幕上进行作画.那我们就来看看这个osgViewe ...

  7. Windows 下 Phpstrom 配置git使用

    首先先去下载 git 下载链接 https://git-scm.com/download/winphpstrom 配置git    链接  http://jingyan.baidu.com/artic ...

  8. qrcode解决方案大全

    QRCODE解决方案 1.delphi原生QRCode.pas 2.delphi编写http服务器实现QRcode请求生成图片,http下载 3.delphi编写cgi程序,加载到apache服务器 ...

  9. Minimum Window Substring LT76

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  10. 7-性能测试i报告

    性能测试报告概述 1.测试报告是指把测试的过程和结果写成文档:对发现的问题和缺陷进行分析:为纠正软件的存在的质量问题提供依据: 为软件验收和交付打下基础 2.性能测试报告属于软件测试报告的一种,主要针 ...