Question

Reference: C# databinding on combobox

I have a similar situation. I have a form with few text boxes, combo boxes, check boxes. Initially I use to catch "changed" event (like, text_changed, selected_index_changed. etc) and sync the UI data with a backing object. I learned about Databinding and I bound all the UI elements to my backing object.

Problem: Based upon state of my UI, I was enabling and disabling "Submit" button which uses backing object to save to contents. But now, due to DataBinding, property of backing object does not change unless the control bound to that properly looses focus.

Question Is there a standard way to solve this problem ? Or do I still need to catch "changed" event and set the value of backing object ?

Also, to make the form fault tolerant, I use to save the backing object every N seconds. But with DataBinding (given that the control which is on focus still havn't set its value to the backing object) I will be saving wrong data.

What I tried Evey N seconds call this.validate() -> this does not work as it actually populate the old data in the control on focus.

----- NOTE ------

The backing object implements INotifyPropertyChanged interface.

and this is how I bind (not in designer) but in code.

Binding binding = new Binding("checked", solution, "InternalFlag"); checkBoxFlag.DataBindings.Add(binding);

Était-ce utile?

La solution

You need change the property of binding which describes when to update the backend object from lost focus to value changed

Data Source Update Mode

binding = new Binding("checked", solution, "InternalFlag", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top