Click here to Skip to main content
14,665,547 members
Rate this:
Please Sign up or sign in to vote.
See more:
In MVVM I can clearly understand communication between View and ViewModel using INotifyPropertyChanged and Commands.

But what about ViewModel to Model communication and vice versa. For me it is unclear what approach is best for MVVM architecture.

Should I just call Model methods from ViewModel directly? And how I shoud track changes in Model - any standart approach?

There is rly little information about Model to ViewModel communication

What I have tried:

Right now I'm just directly calling Model methods from ViewModel. Tried to implement INotifyPropertyChanged on Model but that feels messy
Posted
Updated 6-Mar-18 0:12am
v2

Rate this:
Please Sign up or sign in to vote.

Solution 2

There is nothing wrong in calling directly methods of the Model from the ViewModel.
But the Model shall have no knowledge of the ViewModel.

In order to receive events from the model without breaking the MVVM principle
you could use a so-called Event Aggregator as described here:

Using the Event Aggregator Pattern to Communicate Between View Models - TechNet Articles - United States (English) - TechNet Wiki[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

The model does nothing more than get/save/or delete data from the data source. The view should never directly call any method or refer to any property in the model.

There are many ways to manage this interaction, but presenting a reasonably complete example in Q/A is beyond the intent of Q/A.

Try this youtube lesson:

C# WPF UI Tutorials: 03 - View Model MVVM Basics - YouTube[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100