我在C#中有一个业务对象,它实现了INotifyPropertyChanged并包含几个绑定属性。简而言之,它看起来像这样:
public class BusinessObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private int _intProperty;
public int IntProperty // bound to NumericUpDown control
{
get { return _intProperty; }
set
{
if (_intProperty == value)
{
return;
}
_intProperty = value;
OnPropertyChanged(new PropertyChangedEventArgs("IntProperty"));
// if IntProperty is > 10, then set BoolProperty to false
if (value > 10)
{
this.BoolProperty = false;
//OnPropertyChanged(new PropertyChangedEventArgs("BoolProperty"));
}
}
}
private bool _boolProperty;
public bool BoolProperty // bound to CheckBox
{
get { return _boolProperty; }
set
{
if (_boolProperty == value)
{
return;
}
_boolProperty = value;
OnPropertyChanged(new PropertyChangedEventArgs("BoolProperty"));
}
}
正如您在IntProperty的setter中所看到的,我在设置IntProperty时设置BoolProperty = false> 10. BoolProperty绑定到我的UI(winforms)中的CheckBox,但即使我设置BoolProperty = false,CheckBox也不会更新以反映该更改,直到绑定到IntProperty的控件失去焦点。我想也许我需要在设置BoolProperty = false之后调用OnPropertyChanged,但这似乎没有什么区别。这是这种情况下的预期行为吗?如果是这样,是否有可能实现我所描述的行为?
答案 0 :(得分:1)
您可能需要将绑定的DataSourceUpdateMode
设置为DataSourceUpdateMode.OnPropertyChanged