数据绑定,以便改变一个TextBox值更新列表框 [英] Data Binding so that changing a TextBox value updates the LIstBox

查看:176
本文介绍了数据绑定,以便改变一个TextBox值更新列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的数据更新工作都流入和流出的XAML。即,当我提出在XAML文本框的变化而C#将获得新的价值,当我改变了C#(通过点击一个按钮模拟的),无论是XAML文本框的变化。我有这个工作,但如果我做了更改XAML文本框它doenst更新XAML ItemsList。任何想法我怎么能得到这个工作?



MainWindow.xaml ...

 <窗口x:类=MySimpleProgram.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =725
>
< StackPanel的名称=StackPanel1方向=横向>

<文本框名称=TextBox2中的文本={绑定路径=名字,模式=双向}HEIGHT =23/>

<按钮名称=Button1的CONTENT =更改C#的obj人[0]WIDTH =175HEIGHT =20点击=的button1_Click/>

<列表框名称=listPeople的DisplayMemberPath =名字/>

< / StackPanel的>
< /窗GT;



MainWindow.xaml.cs

 公共类人:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
私人字符串_FirstName;

公共字符串名字
{
{返回_FirstName; }

{
_FirstName =价值;
如果(的PropertyChanged!= NULL)
的PropertyChanged(
本,新PropertyChangedEventArgs(名字));
}
}

公众诠释年龄{搞定;组; }
}

公共部分类主窗口:窗口
{
公众人物[]的人;

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

的人=新的Person [] {
新的Person {名字=雪莉,年龄= 22}
新的Person {名字=罗伊,年龄= 29} ,
新的Person {名字=曼纽尔,年龄= 34}};

StackPanel1.DataContext =人[0];

listPeople.ItemsSource =人;
}


私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
的人[0] .FirstName + =Y;
}

}


解决方案

UpdateSourceTrigger 的PropertyChanged 在文本框结合。



默认值为 引发LostFocus 在失去重心源更新但如果你想,当你在一组输入它的PropertyChanged选择更新。的,你会看到在列表框更新。

 <文本框名称=TextBox2中
文本={绑定路径=名字,模式=双向,
UpdateSourceTrigger = PropertyChanged的}
高度=23/>


I am trying to get data updates to work both into and out of XAML. I.e, when I make a change in the XAML TextBox the C# will receive the new value and when I change the C# (simulated by clicking a button), both the XAML TextBox changes. I have got this to work, however if I make a change to the XAML TextBox it doenst update the XAML ItemsList. Any ideas how I can get this working?

MainWindow.xaml...

<Window x:Class="MySimpleProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="725"
        >
    <StackPanel Name="StackPanel1" Orientation="Horizontal">

        <TextBox Name="TextBox2" Text="{Binding Path=FirstName, Mode=TwoWay}" Height="23"/>

        <Button Name="Button1" Content="Change C# obj people[0]" Width="175" Height="20" Click="Button1_Click" />

        <ListBox Name="listPeople" DisplayMemberPath="FirstName"/>

    </StackPanel>
</Window>

MainWindow.xaml.cs

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private String _FirstName;

    public string FirstName
    {
        get { return _FirstName; }
        set
        {
            _FirstName = value;
            if (PropertyChanged != null)
                PropertyChanged(
                    this, new PropertyChangedEventArgs("FirstName"));
        }
    }

    public int Age { get; set; }
}

public partial class MainWindow : Window
{
    public Person[] people;

    public MainWindow()
    {
        InitializeComponent();

        people = new Person[]{ 
            new Person{ FirstName = "Shirley", Age = 22 }, 
            new Person{ FirstName = "Roy", Age = 29 }, 
            new Person{ FirstName = "Manuel", Age = 34 } };

        StackPanel1.DataContext = people[0];

        listPeople.ItemsSource = people;
    }


    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        people[0].FirstName += "y";
    }

}

解决方案

Set UpdateSourceTrigger to PropertyChanged on your TextBox binding.

Default value is LostFocus i.e. source updates on lost focus but in case you want updation when you typing in set it to PropertyChanged. and you will see update in ListBox.

<TextBox Name="TextBox2"
         Text="{Binding Path=FirstName, Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}"
         Height="23"/>

这篇关于数据绑定,以便改变一个TextBox值更新列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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