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!
This is an old question but still I want to provide an alternative approach for other users who stumble upon this question... In my viewmodels, I do not expose the model properties directly in the get/set Property methods. I use internal variables for all the properties. Then I bind all the properties two-way. So I can do all the validation as "usual" because only the internal variables are changed. In the view model constructor, I have the model object as parameter and I set the internal variables to the values of my model. Now when I click on the "Save" Button (-> Save Command fires in my view model fires) and there are no errors, I set all the properties of my model to the values of the correspondng internal variable. If I click on the "Canel/Undo"-Button (-> Cancel-Command in my view model fires), I set the internal variables to the values of my untouched model (using the setters of the view model properties so that NotifyPropertyChanged is called and the view shows the changes=old values).
Yet another approach would be to implement Memento-Support in the model, so before you start editing you call a function in the model to save the current values, and if you cancel editing you call a function to restore those values...that way you would have the undo/cancel support everywhere an not just in one view model... I've implemented both methods in different projects and both work fine, it depends on the requirements of the project...
If you are using MVVM truely then your OK button click must be handled by some
Command
. This command must be coming from yourViewModel
. TheExpliticly
bound properties must be coming from yourViewModel
again. So whats stopping you.Explicit
binding but useOneWay
binding.OneWay
bound Dependency property.NotifyPropertyChanged
for 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
EmployeeViewModel
class that hasEmployeeName
property 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 typeICommand
namedSaveNameCommand
that return a command for me to execute.EmployeeViewModel
is the data context type of my view. Myview has aTextBox
(named as x:Name="EmployeeNameTxBx")OneWay
bound to theEmployeeName
and a Button asOK
. I bindButton.Command
property toEmployeeViewModel.SaveNameCommand
property andButton.CommandParameter
is bound toEmployeeNameTxBx.Text
property.Inside my
EmployeeViewModel
I 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
EmployeeName
property 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.
IDataErrorInfo
and 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
IDataErrorInfo
works 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
ViewModel
by testing the invalid data. And that data will come if we haveTwoWay
binding enabled. So considering that you are implementingIDataErrorInfo
then you need to haveTwoWay
bindings 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.this
property 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
EmployeeName
will 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
ListMaker
is aIMultiValueConverter
that simply converts values into a list...In your
SpecialBindingBehavior
have aDependentControls
property changed handler...But I will still suggest you use my previous pure MVVM based **Approach 1.