WPF:經過Window.DataContext實現窗口間傳值

經過Window.DataContext實現窗口之間的傳值,特別是跨窗口控件的聯動,具備無可比擬的優點。實現方法以下:express

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

<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.csspa

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.xamlcode

在TextBox.Text上設置了Binding。注意:在設置Binding時沒有指定源,所以,在運行時該綁定將沿着邏輯樹尋找合適的Binding源。orm

<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.csxml

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;
        }
    }
}
相關文章
相關標籤/搜索