Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of the various view creation techniques in WPF?

Tags:

mvvm

wpf

I've been using MVVM for the past two years and it has certainly evolved for the better since that time. As I read though the hundreds of MVVM articles and stackoverflow questions, I've noticed that there appears to be more and more articles that describe the view/viewmodel relationship and creation as ViewModel first or View first. These articles usually use IoC or DI.

My favorite technique has been the use of datatemplates to create the views, and structuring the application off the the viewmodels rather than the views. I rarely see articles anymore that use this pattern.

<DataTemplate DataType="{x:Type ViewModels:DummyViewModel}">
  <Views:DummyUserControl DataContext="{Binding}"/>
</DataTemplate>

Testability and decoupling seem to be the main focus of these 'non-datatemplate' V-VM creation/relationship designs and articles, and often they have to do with MEF or PRISM. Ultimately I would like to know the following:

  1. Is the DataTemplate view creation technique still used or recommended?
  2. What are the pros/cons of the view first design?
  3. What are the pros/cons of the viewmodel first design (with the view injected)

Any good links to articles covering these topics is appreciated, provided they are not MEF/PRISM related.

like image 309
user259509 Avatar asked Jan 26 '10 20:01

user259509


1 Answers

1.Is the DataTemplate view creation technique still used or recommended?

This is my preferred method of operating in MVVM. I like this approach very much, for reasons I'll specify below. I use this in all of my development.

2.What are the pros/cons of the view first design?

The main pro here I've found is that it's a bit easier in the design time experience. The designer "knows" the data context in advance, and tends to be able to do an easier job of working.

The main con here, from my perspective, is that you're adding a tighter coupling between the View and the ViewModel. It's also more difficult to choose a specific model for passing around.

3.What are the pros/cons of the viewmodel first design (with the view injected)

I personally like this approach. This way, the "logical" side of your application is completely contained within your ViewModel layer. By using ContentPresenters, you can have the ViewModel easily generate other ViewModels, defining the "flow" of your application. The Views can be changed by a designer very easily.

The con here, though, is a slight decrease in design time usability, though- since the views don't really know anything about the VM at design time, you lose some designability.

like image 181
Reed Copsey Avatar answered Sep 18 '22 03:09

Reed Copsey