不同窗口中两个文本框之间的数据绑定

本文关键字:文本 两个 之间 数据绑定 窗口 | 更新日期: 2023-09-27 18:35:59

我创建了一个程序,该程序在选中或取消选中复选框时更改文本框中的名称。我想在不同的窗口中复制此文本框。我认为在xaml中使用数据挖掘是可能的,但是该名称仅出现在一个窗口中。第二个窗口窗口不接收数据。我向您展示两个窗口的代码。 你可以帮我吗?谢谢

窗口 1.cs---

namespace WpfApplication1
{
public partial class Window1 : Window
{
    Texto prueba = new Texto("Carlos");

    public static string s;
    public Window1()
    {
       InitializeComponent( );
      // Fill initial person fields
       this.textBox1.Text = prueba.Name;          
    }

    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {

        prueba.Name="Carlos";
        textBox1.DataContext = prueba;
        textBox1.Text = prueba.Name;
    }
    private void checkBox1_UnChecked(object sender, RoutedEventArgs e)
    {
        prueba.Name = "Luis";
        textBox1.DataContext = prueba;
        textBox1.Text = prueba.Name;
    }
}
 public class Texto
{
    string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
     public Texto( ) {}
     public Texto(string name) 
     {
       this.name = name;
     }
}

}

窗口 1 XAML-------

     <Grid>
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="62,118,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_UnChecked" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="44,140,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>

窗口2 cs-----

 namespace WpfApplication1
 {
   public partial class MainWindow : Window
  {
    public MainWindow()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Window1 nueva = new Window1();
        nueva.Show();
    }
 }

}

窗口2 XAML--------

<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="82,121,0,0" Name="button1"  VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox DataContext="prueba" Text="{Binding Path=Name}" Height="23" HorizontalAlignment="Left" Margin="57,84,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
  </Grid>

不同窗口中两个文本框之间的数据绑定

您必须将对第一个窗口或要更新文本属性的对象的引用传递给第二个窗口,DataContext 属性将为此执行此操作,然后您可以将第二个窗口控件绑定到它。

在这个演示应用程序中,我创建了一个主窗口和第二个窗口(Window1),应用程序在主窗口中启动,如下所示。

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public string TestString
    {
        get { return (string)GetValue(TestStringProperty); }
        set { SetValue(TestStringProperty, value); }
    }
    public static readonly DependencyProperty TestStringProperty =  DependencyProperty.Register("TestString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
    public MainWindow()
    {
        InitializeComponent();
        // setup the test string.
        TestString = "this is a test.";
        // Create the second window and pass this window as it's data context.
        Window1 newWindow = new Window1()
        {
            DataContext = this
        };
        newWindow.Show();
    }
}

MainWindow.xaml - 记下 Window 声明中的 DataContext 行。

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <Grid>
        <TextBox Text="{Binding TestString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="91,84,185,189" />
    </Grid>
</Window>

现在对于 Window1,后面的代码只是一个空的默认窗口类,所以我不会发布它,但 xaml 是。

窗口1.xaml

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding TestString, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

不要显式设置 DataContext,也不要仅通过另一个绑定设置。你

<TextBox DataContext="prueba"

没有任何帮助。只要不被覆盖,数据上下文就会被继承。不要显式设置它。在两个窗口上设置一次就足够了。

在主窗口中创建数据对象

Texto prueba = new Texto("Carlos");
Window1 nueva = new Window1();
nueva.DataContext = prueba;
nueva.Show();

并删除所有其他数据上下文分配。

这里有几点错误,但我可能会给你一个快速的解决方案来解决你的问题。 首先,窗口 2 上的 DataContext 无法正常工作,您可以在显示窗口 1 之前在代码中专门设置它...

private void button1_Click(object sender, RoutedEventArgs e)
{
    Window1 nueva = new Window1();
    this.DataContext = nueva.prueba;
    nueva.Show();
}

接下来你必须在你的特克萨斯类中解雇 InotifyPropertyChanged ...

public class Texto : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    string name;
    public string Name
    {
        get { return this.name; }
        set 
        { 
            this.name = value; 
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }
     public Texto( ) {}
     public Texto(string name) 
     {
        this.name = name;
     }
}

如果两个文本框共享一个共同的数据上下文,它将"正常工作",而无需任何代码......