WPF:通过Window.DataContext实现窗口间传值

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

 

posted @ 2017-01-30 17:05  protorock  阅读(4456)  评论(0编辑  收藏  举报