不同窗口中两个文本框之间的数据绑定 [英] Data Binding between two TextBoxes in different windows

查看:40
本文介绍了不同窗口中两个文本框之间的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个程序,它在一个TextBox更名当支票或unckeck的复选框.我想在另一个窗口中复制此文本框.我以为可以在xaml中使用数据挖掘,但是名称仅出现在一个窗口中.第二个窗口窗口不接收数据.我向您展示了两个窗口的代码.你能帮助我吗?谢谢

I have created a program that changes the name in a TextBox when check or unckeck a Checkbox. I want to replicate this Textbox in a different window. I thought with Data Mining in the xaml would be possible, but the name only appears in one window. Second window window does´t recieve the data. I show you the code of the two windows. Can you help me? Thankss

窗口1.cs ---

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

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

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

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>

推荐答案

您将必须将对第一个窗口或要更新text属性的对象的引用传递给第二个窗口,它的DataContext属性将为此,您可以将第二个Windows控件绑定到该窗口.

You will have to pass a reference to either the first window or the object that you're updating the text property on to the second window, it's DataContext property will do for that, you can then bind the second windows controls to it.

在此演示应用程序中,我创建了一个MainWindow和第二个窗口(Window1),该应用程序从这样的Main窗口中启动.

In this demo application I've created a MainWindow and a second window (Window1), the application starts in Main window like this.

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 -注意窗口声明中的DataContext行.

MainWindow.xaml - Take note of the DataContext line in the Window declaration.

<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是.

Now for Window1 the code behind is just an empty default window class so I won't post it but the xaml is.

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>

这篇关于不同窗口中两个文本框之间的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆