2
votes

This is the link i am following for learning MVVM in WPF and i have a question:- https://www.tutorialspoint.com/mvvm/mvvm_first_application.htm

DataContext of the WPF window is set to a VIEWMODEL object. Itemsource of a List-DataTemplate is set to a List from the same VIEWMODEL Object. The Model contains an implementation of INotifyPropertyChanged. When i update the view,the INotifyPropertyChanged of MODEL gets fired ie VIEW is directly updating the MODEL while what i have understood till now is that VIEW interacts with the VIEWMODEL only via Bindings and Commands and never with the MODEL DIRECTLY.It is always the ViewModel which interacts with the Model to fetch data for the View.But here,the View is updating the Model DIRECTLY.This is confusing me owing to my limited knowledge. Please guide.

2

2 Answers

0
votes

If the view model exposes the model through a property, the view may actually bind directly to the model through this property. This doesn't really violate the MVVM pattern and is perfectly fine, especially if the model class implements the INotifyPropertyChanged interface. If it does, you can say that the model is kind of a (child) view model.

A "real" model such as a domain object or a service shouldn't have any knowledge about WPF and how you raise change notifications to a view. Therefore it rarely makes sense to bind directly to such objects but if your models are "WPF aware" and implements view related interfaces, you can bind to them without any issues.

0
votes

It is a common error to bind the Model thru lists to the View. The correct approach would be always to create a ViewModel of that Model (the list element) and bind to it.

For example:

enter image description here

Otherwise you are opening the door to including data on ModelB that should be stored in the ViewModelB.