【问题标题】:Data Binding between two TextBoxes in different windows不同窗口中两个文本框之间的数据绑定
【发布时间】:2012-05-31 15:18:28
【问题描述】:

我创建了一个程序,在选中或取消选中复选框时更改文本框中的名称。我想在不同的窗口中复制这个文本框。我认为在 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;
     }

}


}

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>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您必须将对第一个窗口或您正在更新文本属性的对象的引用传递给第二个窗口,它的 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 是。

    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 属性时引发事件,这将同样有效。
    【解决方案2】:

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

    <TextBox DataContext="prueba"
    

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

    在主窗口中创建数据对象

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

    并删除所有其他 DataContext 分配。

    【讨论】:

      【解决方案3】:

      这里有一些问题,但我可能会为您提供快速解决方案来解决您的问题。首先,窗口 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;
           }
      
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-19
          • 1970-01-01
          • 2015-02-10
          • 2023-03-29
          相关资源
          最近更新 更多