Kri*_*son 6 .net data-binding winforms
我有一个实现INotifyPropertyChanged的对象,以及一个绑定到该对象的布尔属性的复选框.这是有效的,但是我发现当我选中或取消选中该复选框时,在我单击另一个控件,关闭表单或以其他方式使复选框失去焦点之前,对象的绑定属性不会更新.
我希望复选框立即生效.也就是说,当我选中该框时,该属性应立即设置为true,当我取消选中该框时,应立即将其设置为false.
我通过为复选框的CheckedChanged事件添加一个处理程序来解决这个问题,但是有一个"正确的方法"来做这个我忽略的事情吗?
类似的Stack Overflow问题是文本框/复选框的数据绑定值不正确,直到验证文本框/复选框.
将绑定模式设置为OnPropertyChanged:
this.objectTestBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.objectTestBindingSource.DataSource = typeof(WindowsFormsApplication1.ObjectTest);
this.checkBox1.DataBindings.Add(
new System.Windows.Forms.Binding(
"Checked",
this.objectTestBindingSource,
"SomeValue",
true,
System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
public class ObjectTest: System.ComponentModel.INotifyPropertyChanged
{
public bool SomeValue
{
get { return _SomeValue; }
set { _SomeValue = value; OnPropertyChanged("SomeValue"); }
}
private bool _SomeValue;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (string.IsNullOrEmpty(name)) {
throw new ArgumentNullException("name");
}
if (PropertyChanged != null) {
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
ObjectTest t = new ObjectTest();
this.objectTestBindingSource.Add(t);
}
Run Code Online (Sandbox Code Playgroud)
一旦我点击该框,这就有效.
| 归档时间: |
|
| 查看次数: |
2949 次 |
| 最近记录: |