简体   繁体   中英

Validating controls bound to different properties of the same object resets objectvalues

I have a form on which all textboxes are bound to different properties of the same dataobject that implements INotifyPropertyChanged. The Forms Autovalidate is set to 'Disable' as I want to trigger the validation explicitly by calling form.ValidateChildren() .

Expected: After calling ValidateChildren all edited values should be in my dataobject.

Problem: Only the last focused control writes it's data to the dataobject, but all other controls lose the edited values and show the old value instead.

Question: How can I make sure that all data is validated before the controls refresh themselves?

Using Autovalidate = EnablePreventFocusChange or EnableAllowFocusChange does work but as I want to validate all at once it is not an acceptable solution for me.

Searching the internet for soutions I found an example showing the same problem but unfortunately no solution.

EDIT After further investigation i tried this and it works:

form.BindingContext[dataobject].SuspendBinding();
form.ValidateChildren();
form.BindingContext[dataobject].ResumeBinding();

Is Pausing the Binding the standard way or are there any better solutions to fix this?

I'm not sure if this is helpful or will it be a complete answer but I had similar problem where only one value has been saved out of whole form. Here is some background explanation I found on the MS site:

http://connect.microsoft.com/VisualStudio/feedback/details/351146/binding-writevalue-does-not-update-underlying-data-source

Even SuspendBinding didn't work for me. So following the link and the information there I connected the objects via BindingSource object with RaiseListChangeEvents property set to False

Dim MyBindingSource = New BindingSource With {.DataSource = MyDataSource, .RaiseListChangedEvents = False}
MyControl.DataSource = MyBindingSource

Once this is in place you can call refer to the binding on the control and write the value manually (assuming there is just one binding) the code will look similar to:

MyControl.Bindings(0).WriteValue()

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM