Search code examples
c#wpfmvvm

When using DataTemplate in mvvm, does the view need to know the model?


In MVVM, should the View know about the model?

As zzfima answered in the example above, you can use something like DataTemplete.

In fact, when the property "Name" of Persons is changed to "PersonName", the XAML of the View must also change.

The correct mvvm structure I know of is:

enter image description here

But, as above, if the view knows the model, then the structure is: enter image description here

Can we really say that the view doesn't know the model?

Assuming it knows, is there a way to "completely" isolate the view so that it doesn't know the model?


Solution

  • It depends on how much you are really willing to work on separation! All the examples in tutorials are not made to make full separation, they just wanna show working example.

    If you use your domain models inside the ViewModel then the View knows about them. If you need to separate that - then u gonna have mapper that will map your domain models into the ViewModels that are only known to View!

    The idea for MVVM is that you can swap your view easy and to have more control inside the ViewModel iteself.

    Let's assume you have your Customer Order domain objects with logic inside of them , if u wanna present them to the View you gonna have CustomerViewModel and OrderViewModel which will be mapped from domain objects. And your OrderDetailsViewModel will be :

    public class OrderDetailsViewModel
    
     { 
       public class CustomerViewModel {get; set;}
       public class OrderViewModel {get; set;}
     }