Maurizio Reginelli
Maurizio Reginelli

Reputation: 3222

M-V-VM best practices

I found two ways to use M-V-VM pattern in WPF:

  1. allocate the ViewModel into the View's code behind (setting it as the DataContext);
  2. allocate the ViewModel into a XAML file and create the corresponding view using a DataTemplate.

The Model can be allocated into the ViewModel's constructor.

What do you think about this way of using M-V-VM pattern? What are best practices relative to it?
Thank you

Upvotes: 4

Views: 283

Answers (2)

Sean B
Sean B

Reputation: 353

Method #3 is what we do:

This is with Boo / Binsor... the datacontext is setup through IOC.

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

Upvotes: 1

user7116
user7116

Reputation: 64148

I tend to follow #2 and have found it to be the most flexible. As a best practice I would move these DataTemplate's into ResourceDictionary's so they can be shared amongst XAML that will require them.

Upvotes: 1

Related Questions