数据在不同窗口中的两个TextBox之间绑定

时间:2012-05-31 15:18:29

标签: c# wpf xaml

我创建了一个程序,在检查或取消检查复选框时更改TextBox中的名称。我想在另一个窗口中复制此文本框。我认为在xaml中使用Data Mining是可能的,但名称只出现在一个窗口中。第二个窗口窗口不接收数据。我告诉你两个窗口的代码。  你能帮助我吗? Thankss

Window 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;
     }

}


}

window1 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>

window2 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();
    }
 }


}

window2 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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

不要显式设置DataContext,也不要只通过另一个Binding设置。你的

<TextBox DataContext="prueba"

没有任何帮助。只要不覆盖DataContext,它就会被继承。不要明确设置它。应该在两个窗口上设置一次。

在MainWindow中创建数据对象

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

并删除所有其他DataContext分配。

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:0)

如果两个文本框共享一个公共的datacontext,它将“正常工作”而不需要任何代码...