我有一个存储 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
在表单之外,然后它显示在 UI 中。
如果我修改this.Food
,它更新正确。如果我以编程方式修改 UI,如下所示:
this.textBox.Text = "NewFruit"
,它不会更新 this.Food。
为什么会这样呢?我也实施了INotifyPropertyChanged
对于 Fruit.Name,但仍然相同。
我建议您实现 INotifyPropertyChanged 并将数据绑定代码更改为:
this.textBox.DataBindings.Add("Text",
this.Food,
"Name",
false,
DataSourceUpdateMode.OnPropertyChanged);
这样就可以解决问题了。
请注意,默认DataSourceUpdateMode
is OnValidation
,所以如果你不指定OnPropertyChanged
,直到验证发生后模型对象才会更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)