Force binding data-bound window forms to change the property value immediately after clicking

I have an object that implements INotifyPropertyChanged and a checkbox bound to the boolean property of this object. This works, but I found that when checking or unchecking the checkbox, the bound property of the object is not updated until I click on another control, close the form, or make the checkbox lose focus.

I want the flag to take effect immediately. That is, when I check the box, the property should be set to true immediately, and when I clear the box, it should be set to false immediately.

I worked on this by adding a handler for the CheckedChanged checkbox, but is there a β€œright path” for this that I skipped?


A similar question. The value of binding to the text box / checkbox is incorrect until the checkbox in the text box / is confirmed.

+6
data-binding winforms
source share
1 answer

Set the binding mode to 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); } 

It works as soon as I click on the field.

+6
source share

All Articles