如何跨窗口绑定元素



有两个窗口:MainWindow和Window1。

下面是 MainWindow 的 XAML:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <TextBox x:Name="TB"></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
        <TextBox></TextBox>
    </Grid>
</Window>

以下是 MainWindow 的代码隐藏:

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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Window1 W = new Window1(this);
            W.Show();
        }
    }
}

下面是窗口 1 的 XAML:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <TextBox Text="{Binding Text,ElementName=TB,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>

以下是 Window1 的代码隐藏:

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 WpfApp1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1(MainWindow MW)
        {
            InitializeComponent();
            this.DataContext = MW;
        }
    }
}

我想将 Window1 的TextBox绑定到X:Name="TB"主窗口中的TextBox

我该怎么做?为什么我的代码不起作用?

你能帮帮我吗?谢谢。

为什么我的代码不起作用?

Window1中没有要绑定到的名为"TB"的元素。

如果将属性添加到 MainWindow ,则可以绑定到此属性:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Window1 W = new Window1(this);
        W.Show();
    }

    public string TheText
    {
        get { return TB.Text; }
        set { TB.Text = value; }
    }
}

窗口 1 中的 XAML:

<TextBox Text="{Binding TheText, UpdateSourceTrigger=PropertyChanged}" />

您可能希望创建一个在窗口之间共享的视图模型,而不是注入Window1引用MainWindow但这是另一回事。

创建一个 ViewModel 并与 Windows DataContext 共享

视图模型:

// NB: Not the Perfect or complete solution to Implement INotifyPropertyChanged
// Don't use it in Production Code.
public class ViewModel : INotifyPropertyChanged
{
    #region Notify Property Changes
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string pname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pname));
    }
    #endregion
    private string _Text;
    public string Text
    {
        get { return _Text; }
        set
        {
            if (value != _Text)
            {
                _Text = value;
                OnPropertyChanged("Text");
            }
        }
    }
}

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    private ViewModel ViewModel;
    public MainWindow()
    {
        ViewModel = new ViewModel();
        DataContext = ViewModel;
        InitializeComponent(); 
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Window1 W = new Window1() { DataContext = ViewModel};
        W.Show();
    }
}

MainWindow.xaml

<Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
    <TextBox Text="{Binding Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</Grid>

最新更新