相关疑难解决方法(0)

TextBox的数据绑定

我有一个基本属性存储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");
}
Run Code Online (Sandbox Code Playgroud)

所以我this.Food在表单外面设置然后它显示在UI中.

如果我修改this.Food,它会正确更新.如果我以编程方式修改UI,如:

this.textBox.Text = "NewFruit",它没有更新this.Food.

为什么会这样?我也INotifyPropertyChanged为Fruit.Name 实现了,但仍然是一样的.

.net c# data-binding winforms

29
推荐指数
1
解决办法
9万
查看次数

标签 统计

.net ×1

c# ×1

data-binding ×1

winforms ×1