I saw this example – Binding.UpdateSourceTrigger Property
in the example the UpdateSourceTrigger set to Explicit and then in the view code he call to UpdateSource of the TextBox name.
But if i use the MVVM dp i dont want to have names to my controls and source properties are in the VM and not in the view so what is the right way to bind controls to VM properties and set the UpdateSourceTrigger to explicit?
I want to do this because in my case its ShowDialog window and I want that the source will update only if the user click “ok”
Thanks in advance!
If you are using MVVM truely then your OK button click must be handled by some
Command. This command must be coming from yourViewModel. TheExpliticlybound properties must be coming from yourViewModelagain. So whats stopping you.Explicitbinding but useOneWaybinding.OneWaybound Dependency property.NotifyPropertyChangedfor that property from yourViewModel.E.g.
Assume I need to update a TextBox’s Text back into my model on OK button click.
So for that I have a
EmployeeViewModelclass that hasEmployeeNameproperty in it. The property is has a getter and a setter. The setter raises property changed notification. The view model also has another property of typeICommandnamedSaveNameCommandthat return a command for me to execute.EmployeeViewModelis the data context type of my view. Myview has aTextBox(named as x:Name=”EmployeeNameTxBx”)OneWaybound to theEmployeeNameand a Button asOK. I bindButton.Commandproperty toEmployeeViewModel.SaveNameCommandproperty andButton.CommandParameteris bound toEmployeeNameTxBx.Textproperty.Inside my
EmployeeViewModelI haveOnSaveNameCommandExecute(object param)method to execute mySaveNameCommand.In this perform this code…
This way ONLY OK button click, updates the TextBox’s text back into
EmployeeNameproperty of the model.EDIT
Looking at your comments below, I see that you are trying to implement Validation on a UI. Now this changes things a little bit.
IDataErrorInfoand related validation works ONLY IF your input controls (such as TextBoxes) are TwoWay bound. Yes thats how it is intended. So now you may ask “Does this mean the whole concept of NOT ALLOWING invalid data to pass to model is futile in MVVM if we use IDataErrorInfo”?Not actually!
See MVVM does not enforce a rule that ONLY valid data should come back. It accept invalid data and that is how
IDataErrorInfoworks and raises error notfications. The point is ViewModel is a mere softcopy of your View so it can be dirty. What it should make sure is that this dirtiness is not committed to your external interfaces such as services or data base.Such invalid data flow should be restricted by the
ViewModelby testing the invalid data. And that data will come if we haveTwoWaybinding enabled. So considering that you are implementingIDataErrorInfothen you need to haveTwoWaybindings which is perfectly allowed in MVVM.Approach 1:
What if I wan to explicitly validate certain items on the UI on button click?
For this use a delayed validation trick. In your ViewModel have a flag called isValidating. Set it false by default.
In your
IDataErrorInfo.thisproperty skip the validation by checking isValidating flag…Then in your OK command executed handler, check employee name and then raise property change notification events for the same property …
This triggers the validation ONLY when you click OK. Remember that
EmployeeNamewill HAVE to contain invalid data for the validation to work.Approach 2:
What if I want to explicitly update bindings without TwoWay mode in MVVM?
Then you will have to use
Attached Behavior. The behavior will attach to the OK button and will accept list of all items that need their bindings refreshed.The
ListMakeris aIMultiValueConverterthat simply converts values into a list…In your
SpecialBindingBehaviorhave aDependentControlsproperty changed handler…But I will still suggest you use my previous pure MVVM based **Approach 1.