Best Practices MV-VM

I found two ways to use the MV-VM pattern in WPF:

  • highlights the ViewModel in the view code behind (setting it as a DataContext);
  • highlight the ViewModel in the XAML file and create the corresponding view using the DataTemplate.

The model can be selected in the ViewModel constructor.

What do you think of this way of using the MV-VM pattern? What are the best practices regarding this?
Thanks you

+4
source share
2 answers

I try to follow No. 2 and consider it the most flexible. As a best practice, I would move these DataTemplate to a ResourceDictionary so that they can be shared between XAMLs that require them.

+1
source

Method No. 3 is what we do:

This is with Boo / Binsor ... datacontext is set via IOC.

 component "AngleRoomModel", IRoomViewModel, AngleRoomViewModel component "AngleRoom", IRoomView, AngleRoomView: DataContext = @AngleRoomModel 
+1
source

Source: https://habr.com/ru/post/1310164/


All Articles