通过Window.DataContext实现窗口之间的传值,特别是跨窗口控件的联动,具有无可比拟的优势。实现方法如下:

1.  MainWindow.xaml,在Window.DataContext中声明Binding,Binding的源是窗口的控件

<Window x:Class="WpfAppInterWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppInterWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<!--添加Binding,Binding源为指定的控件-->
<Binding ElementName="textBox"/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="22"/>
</Grid.RowDefinitions>
<WrapPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="输入:" />
<TextBox x:Name="textBox" Width="200" />
</WrapPanel>
<Button Grid.Row="1" Content="显示Window1" Click="Button_Click" />
</Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfAppInterWindow
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
// 显示Widnow1
Window1 w1 = new Window1(this);
w1.Show();
}
}
}

2. Window1.xaml

在TextBox.Text上设置了Binding。注意:在设置Binding时没有指定源,因此,在运行时该绑定将沿着逻辑树寻找合适的Binding源。

<Window x:Class="WpfAppInterWindow.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppInterWindow"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Grid>
<WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox Text="{Binding Path=Text, UpdateSourceTrigger=PropertyChanged}" Width="200"/>
</WrapPanel>
</Grid>
</Window>

Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes; namespace WpfAppInterWindow
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
} public Window1(MainWindow mainWindow) : this()
{
// 设置本窗口的DataContext。为TextBox的绑定提供源
this.DataContext = mainWindow.DataContext;
}
}
}

WPF:通过Window.DataContext实现窗口间传值的更多相关文章

  1. C# WPF 通过委托实现多窗口间的传值

    在使用WPF开发的时候就不免会遇到需要两个窗口间进行传值操作,当然多窗口间传值的方法有很多种,本文介绍的是使用委托实现多窗口间的传值. 在上代码之前呢,先简单介绍一下什么是C#中的委托(如果只想了解如 ...

  2. WPF中实现两个窗口之间传值

    在使用WPF的时候,我们经常会用到窗体之间传值,下面示例主窗口传值到子窗口,子窗口传值到主窗口的方法. 一.主窗口向子窗口传值 主窗口向子窗口传值主要方法就是在子窗口建立一个接收主窗口值的变量,然后实 ...

  3. Chrome调试本地文件无法使用window.opener对象进行窗口间值传递

    今天在百度BAE上建了个应用,svn上传后发现页面间互调有些问题,几经查询发现: (1)IE下正常的window.opener.object1.object2页面间对象访问方法在Chrome下不能使用 ...

  4. WPF设置Window的数据上下文(DataContext)为自身

    WPF设置Window的数据上下文(DataContext)为自身的XAML: DataContext="{Binding RelativeSource={RelativeSource Se ...

  5. Pyqt 窗体间传值

    窗体间传值网上有好多方法,比如新建文件,先将子类窗体的数据传到文件中,父窗体读取文件.  Signal&Slot机制进行传值 等等 在这里,我们就举个采用apply方法:Signal& ...

  6. JAVASCRIPT实现的WEB页面跳转以及页面间传值方法

    在WEB页面中,我们实现页面跳转的方法通常是用LINK,BUTTON LINK ,IMG LINK等等,由用户点击某处,然后直接由浏览器帮我们跳转. 但有时候,需要当某事件触发时,我们先做一些操作,然 ...

  7. 【WPF】运用MEF实现窗口的动态扩展

    若干年前,老周写了几篇有关MEF的烂文,简单地说,MEF是一种动态扩展技术,比如可以指定以某个程序集或某个目录为搜索范围,应用程序在运行时会自动搜索符合条件的类型,并自动完成导入,这样做的好处是,主程 ...

  8. WPF如何仿制QQ2013登录窗口的关闭效果

    昨天,有位朋友问我,WPF能做出像QQ2013窗口在关闭时那个貌似透明过渡的动画吗?我就歪着脸跟他说:"只有你想不到的,没有WPF做不到的". 他又接着说:"我知道肯定会 ...

  9. node-webkit 新建实例窗口间通信问题解决办法

    终于弄明白这问题了,只要在js文件里加上段代码,就可解决两窗口间通信问题. var str = { username: User.name, userrole: User.role }; var ne ...

随机推荐

  1. (转)Hibernate的一级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/70148567 Hibernate的一级缓存 Hibernate的一级缓存就是指Session缓 ...

  2. 零基础学习Python培训,应该选择哪个培训班?

    近几年中,Python一直是市场上最受欢迎的编程语言之一.它语法自然,入门简单,同时应用范围又极广,无论是大火的人工智能.大数据还是传统的web开发.自动化运维,Python都能够大展拳脚.根据职友集 ...

  3. linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号

    linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号 实现的功能是吧换行符换成逗号了,自己试验过. 求解释,:t N b t 都是什么意思??? :t 定义la ...

  4. C++ Primer(第4版)-学习笔记-第1部分:基本语言

    第1章  快速入门 每个C++程序都包含一个或多个函数,而且必须有一个命名为main. main函数是唯一被操作系统显式调用的函数,main函数的返回值必须是int或者void(无返回值) 函数体是函 ...

  5. Django——7 常用的查询 常用的模型字段类型 Field的常用参数 表关系的实现

    Django 常用的查询 常用的查询方法 常用的查询条件 常用字段映射关系 Field常用参数 表关系的实现 查用的查询方法 这是需要用到的数据 from django.http import Htt ...

  6. codeforeces近日题目小结

    题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...

  7. BUPT2017 springtraining(16) #4 ——基础数论

    题目在这里 A.手动打表找规律得组合数 n -= 2, m -= 2, ans = C(n, m) #include <bits/stdc++.h> using namespace std ...

  8. 【ACM】nyoj_106_背包问题_201308152026

    背包问题时间限制:3000 ms  |  内存限制:65535 KB 难度:3描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10 ...

  9. 猫猫学iOS之UILabel设置圆角不成功所做调控更改

    原创文章.欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 如图问题 如图是我要做的效果 然而当我写好代码后,设置号label的layer圆角后 ...

  10. 大神note3千元指纹机,这是要逼疯友商吗

    新发现(光山居士).7月20日下午.奇酷公司在北京奥雅会展中心召开公布会,宣布推出首款千元级别的指纹识别机大神Note3.据悉.该型号手机.移动版售价899元.全网通版售1099元,并在16:00開始 ...