我有一个存储类型水果的对象的基本属性:
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);
这会解决它。