在不同的Windows中两个TexBoxes之间的数据绑定 [英] Data Binding between two TexBoxes in different windows

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

问题描述

我创建了一个程序,在一个TextBox更名时,支票或unckeck的复选框。我想在不同的窗口复制此文本框。我认为数据挖掘在XAML是可能的,但是名字只出现在一个窗口。第二个窗口的窗口简化版,收到的数据。我告诉你两个窗口的代码。
你能帮助我吗? Thankss



窗口1.cs ---

 命名空间WpfApplication1 
{

公共部分类窗口1:窗口
{
Texto prueba =新Texto(卡洛斯);


公共静态字符串s;
公共窗口1()
{
的InitializeComponent();
//初始填充字段人
this.textBox1.Text = prueba.Name;

}


私人无效checkBox1_Checked(对象发件人,RoutedEventArgs E)
{


prueba。 NAME =卡洛斯;
textBox1.DataContext = prueba;
textBox1.Text = prueba.Name;
}

私人无效checkBox1_UnChecked(对象发件人,RoutedEventArgs E)
{
prueba.Name =路易斯;
textBox1.DataContext = prueba;
textBox1.Text = prueba.Name;
}
}

公共类Texto
{
字符串名称;
公共字符串名称
{
{返回this.name; }
集合{this.name =值; }
}

公共Texto(){}
公共Texto(字符串名称)
{
this.name =名称;
}

}


}

窗口1 XAML -------

 <网格和GT; 
<复选框CONTENT =复选框HEIGHT =16的Horizo​​ntalAlignment =左保证金=62,118,0,0NAME =checkBox1VerticalAlignment =评出的选中=checkBox1_Checked未选中=checkBox1_UnChecked />
<文本框高度=23的Horizo​​ntalAlignment =左保证金=44,140,​​0,0NAME =textBox1的VerticalAlignment =评出的WIDTH =120/>
< /网格和GT;



窗口2 CS -----

 命名空间WpfApplication1 
{

公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
}

私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
窗口1努埃瓦=新窗口1();
nueva.Show();
}
}


}

窗口2 XAML --------

 <网格和GT; 
<按钮内容=按钮HEIGHT =23的Horizo​​ntalAlignment =左保证金=82,121,0,0NAME =Button1的VerticalAlignment =评出的WIDTH =75点击=的button1_Click />
<文本框的DataContext =prueba文本={绑定路径=名称}HEIGHT =23的Horizo​​ntalAlignment =左保证金=57,84,0,0NAME =TextBox2中VerticalAlignment = 顶级WIDTH =120/>
< /网格和GT;


解决方案

您将有一个引用传递到第一窗口或者说你在第二个窗口更新文本属性的对象,它的DataContext属性将做,那么你可以将绑定在第二个窗口控制它。



在我创建了一个主窗口和第二窗口(窗口1)此演示应用程序,该应用程序在开始这样的主窗口。



MainWindow.xaml的.cs

 公共部分类主窗口:窗口
{
公共字符串的TestString
{
{返回(串)的GetValue(TestStringProperty); }
集合{的SetValue(TestStringProperty,值); }
}

公共静态只读的DependencyProperty TestStringProperty = DependencyProperty.Register(的TestString的typeof(串)的typeof(主窗口),新UIPropertyMetadata(NULL));

公共主窗口()
{
的InitializeComponent();

//设置测试字符串。
的TestString =这是一个考验。

//创建第二个窗口,通过这个窗口,因为它的数据上下文。
窗口1 newWindow =新窗口1()
{
的DataContext =这个
};
newWindow.Show();
}
}



MainWindow.xaml - 。记在Window声明DataContext的行

 <窗​​口x:类=WpfApplication5.MainWindow
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525
的DataContext ={绑定的RelativeSource = {自我的RelativeSource}}
>
<网格和GT;
<文本框的文本={结合的TestString,模式=双向,UpdateSourceTrigger =的PropertyChanged}保证金=91,84,185,189/>
< /网格和GT;
< /窗GT;

现在为窗口1后面的代码只是一个空的默认窗口类,所以我将不会发布,但在XAML是。



Window1.xaml

 <窗​​口x:类=WpfApplication5.Window1
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP ://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<网格和GT;
< TextBlock的文本={结合的TestString,UpdateSourceTrigger =的PropertyChanged}/>
< /网格和GT;
< /窗GT;


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

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>

解决方案

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.

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

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>

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

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