通过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>