Update ViewModel from View

2019-08-26 11:03发布

问题:

Is there any event or way to know the trigger when the ViewModel is updating/updated by the View?

INotifyPropertyChanged and PropertyChanged is used when the ViewModel updated the View.

But is there any reverse?

回答1:

Changes from the view to the view model will trigger PropertyChanged as well, as they set properties.



回答2:

If your are binding the controls in your view to the view model, many of the pieces will update the view model as long as you make sure the binding is Mode=TwoWay.

http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx

if for some reason you have set your UpdateSourceTrigger in the binding to be Explicit then to update the viewmodel you will need to get the binding expression and call update source

http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.getbindingexpression.aspx



回答3:

If you are using binding then you can use Binding.SourceUpdated and Binding.TargetUpdated Attached Events.