tags:

views:

113

answers:

2

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

+1  A: 

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.

sixlettervariables
+1  A: 

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
Sean B
We are using the DI container, all the view has to know is the contract/convention that the VM implements and the container takes care of the pairing of the two. You can also use this technique if you are using automated View testing, by having the DI container swap real VMs for test VMs.Using a Caliburn level framework neither the View or the VM even reference eachother, instead the Framework handles the pairing automatically based on convention.
Agies
Nice job! I found the Cailburn project you're talking about: http://caliburn.codeplex.com/
Sean B