通过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. IIS中实现http自动转换到https

    IIS中实现http自动转换到https修改以下文件:C:\WINDOWS\Help\iisHelp\common\403-4.htm 为以下内容<!DOCTYPE HTML PUBLIC &q ...

  2. PowerDesigner16逆向工程生成PDM列注释(My Sql5.0模版)

    一.编辑当前DataBase 选择DataBase——>edit Current DBMS...弹出如下对话框:  如上图,先解释一下: 根据红颜色框从上往下解释一下. 第一个红框是对应的修改的 ...

  3. XML在线转化为JSON

    http://www.utilities-online.info/xmltojson/

  4. vue04 组件化开发 Vue自动化工具

    5. 组件化开发 5.1 组件[component] 组件(Component)是自定义封装的功能.在前端开发过程中,经常出现多个网页的功能是重复的,而且很多不同的网站之间,也存在同样的功能. 而在网 ...

  5. 【实战】用request爬取拉勾网职位信息

    from urllib import request import urllib import ssl import json url = 'https://www.lagou.com/jobs/po ...

  6. SqlServer IsNull 与 NullIf

    ISNULL(check_expression, replacement_value) check_expression 与 replacement_value 数据类型必须一致,如果 check_e ...

  7. 第三节:Web爬虫之BeautifulSoup解析库

    Beautiful Soup官方说明: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为 ...

  8. 第二节:Series基本属性及方法(下)

  9. windows桌面远程工具连接Ubuntu

    1.Ubuntu安装:sudo apt-get install xrdp    sudo apt-get install vnc4server sudo apt-get install xubuntu ...

  10. [网络收集]20190528华为数通网络工程师认证HCIA-VRP的操作指导思维导图

    >> >0 VRP基础 >1 命令行基础 >2 文件系统基础 >3 VRP系统管理