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

3
我已经创建了一个程序,当勾选或取消复选框时,可以更改文本框中的名称。我想在不同的窗口中复制这个文本框。我认为通过 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>
4个回答

4
你需要将文本属性更新的对象的引用传递到第二个窗口,可以使用DataContext属性来实现,然后将第二个窗口的控件绑定到它。 在这个演示应用程序中,我创建了一个MainWindow和第二个窗口(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文件是存在的。 Window1.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>

作为额外提示,如果您对依赖属性不熟悉,可能会感到有些困惑。但是,如果您实现了INotifyPropertyChanged接口,并在更改TestString属性时引发事件,这也可以正常工作。 - Andy

1
不要显式设置DataContext,也不要仅通过另一个绑定来设置。您的

<TextBox DataContext="prueba"

这并没有什么帮助。只要DataContext没有被覆盖,它就会被继承。不要明确设置它。在两个窗口上设置一次就足够了。

在你的MainWindow中创建你的数据对象

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

并删除所有其他DataContext分配。


0
如果两个文本框共享一个公共数据上下文,它将“自动工作”,无需任何代码...

0
这里有几个问题,但我可以给你一个快速解决方案来修复你的问题。首先,你在窗口2上的DataContext没有正常工作,你可以在显示窗口1之前在代码中将其设置为独占...
private void button1_Click(object sender, RoutedEventArgs e)
{
    Window1 nueva = new Window1();
    this.DataContext = nueva.prueba;
    nueva.Show();
}

接下来,您需要在您的Texto类中触发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;
     }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接