c# - 不同窗口中两个文本框之间的数据绑定(bind)

标签 c# wpf xaml

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

最佳答案

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

在这个演示应用程序中,我创建了一个主窗口和第二个窗口 (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>

关于c# - 不同窗口中两个文本框之间的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10836431/

相关文章:

silverlight - Silverlight 2 中的不可变样式

c# - 将 PropertyChangedCallback 标记为异步是否安全?

c# - 是否可以部署多个版本的 UWP 应用程序供用户同时运行?

wpf - 需要帮助对齐 WPF 中的控件

c# - MySql 无法连接到 datagrid wpf

wpf - 使用 Storyboard的动态动画

c# - 如何使 TextBlock 在 GridViewColumn 中居中? (包括示例 xaml)

c# - ListView跳到顶部(ISupportIncrementalLoading)

c# - ASP.net MVC Entity Framework 更新多对多关系

c# - 在“链接”按钮的单击事件中应用 Jquery 函数(在代码后面)