数据绑定的TextBox [英] Data binding for TextBox

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

问题描述

我有一个存储类型水果的对象的基本属性:

I have a basic property that stores an object of type Fruit:

Fruit food;
public Fruit Food
{
    get {return this.food;}
    set
    {
    	this.food= value;
    	this.RefreshDataBindings();
    }
}

public void RefreshDataBindings()
{
    this.textBox.DataBindings.Clear();
    this.textBox.DataBindings.Add("Text", this.Food, "Name");
}

所以我设置 this.Food 之外的形式,然后将其显示在用户界面。

So I set this.Food outside the form and then it shows up in the UI.

如果我修改 this.Food ,它更新正确。如果我修改了用户界面编程,如:

If I modify this.Food, it updates correctly. If I modify the UI programmatically like:

this.textBox.Text =NewFruit,它不更新this.Food。

this.textBox.Text = "NewFruit", it doesn't update this.Food.

为什么会这样?我还实施 INotifyPropertyChanged的的Fruit.Name,但还是一样。

Why could this be? I also implemented INotifyPropertyChanged for Fruit.Name, but still the same.

推荐答案

我建议你实现INotifyPropertyChanged和更改绑定code到这一点:

I Recommend you implement INotifyPropertyChanged and change your databinding code to this:

this.textBox.DataBindings.Add("Text",
                                this.Food,
                                "Name",
                                false,
                                DataSourceUpdateMode.OnPropertyChanged);

这会解决它。

这篇关于数据绑定的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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