6

As I read here:

http://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx

There is typically a one-to-one relationship between a view and its view model.

It means that by design they don't really cope with multiple views ?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 1
    Actually multiple views can use one view model (different types of charts, for example). Also these views can be switched by using DataTemplateSelector, here is my answer to another question where I created an example of this situatuon: http://stackoverflow.com/questions/5309099/changing-the-view-for-a-viewmodel/5310213#5310213 – vortexwolf Apr 09 '11 at 19:19
  • thanks will read it but I have a problem with datatemplateselector http://stackoverflow.com/questions/5607619/how-mvvm-with-datatemplateselector-is-portable-from-silverlight-to-flex – user310291 Apr 09 '11 at 20:21

5 Answers5

5

I think this is usually the case in practice. However, the beauty of separating the presentation into a View and a ViewModel means that you could easily create many different Views, each showing basically the same data from the Model, all sharing the same ViewModel class (maybe or maybe not the same instance). For example, I could have a simple and advanced View of my data, written mostly in XAML as two completely different UserControl's, both sharing the same ViewModel (class or maybe instance). Without using MVVM this would be trickier to do without duplicating code.

StellarEleven
  • 556
  • 4
  • 8
4

I think there is no restriction of this, it totally depends on your design and requirement. You can create multiple View for a single ViewModel to present different UI representation.

pchajer
  • 1,584
  • 2
  • 13
  • 25
  • This suggests that the ViewModel needs to be created before the View but in many example you see the View is created first. In WinForms using Application.Run(new MainForm())) does not make it easy to create the ViewModel first – Paul McCarthy Mar 04 '19 at 11:18
2

In MVVM, you have a View (presentation) and a ViewModel (logic) which is intended to support that presentations needs. You can easily have multiple views for a ViewModel, and often this is expected, mostly in the case where you have multiple DataTemplate objects (which is a view) bound to a single ViewModel type based on the context in which it is being used.

While there is typically a one to one mapping of these, this is not a limitation but a convention and there is no "design limitation" in the MVVM pattern.

Dave White
  • 3,451
  • 1
  • 20
  • 25
  • how is it a convention if one has to use DataTemplate selector to get multiple views ? This isn't probably portable http://stackoverflow.com/questions/5607619/how-mvvm-with-datatemplateselector-is-portable-from-silverlight-to-flex – user310291 Apr 09 '11 at 20:19
  • The one to one mapping of Views to ViewModels is the convention. The means by which you implement a Many to One mapping is up to you. – Dave White Apr 09 '11 at 20:24
2

Correct. Typically, view model is designed specifically for one view. It doesn't have any knowledge about specific controls used in the view, but it does have the structural and functional knowledge. Having multiple views and one view model would often make the view model class violate the single responsibility principle.

Although, sometimes it does make sense to have several views and one view model. For instance, basic and advanced version of the same view. In basic version you just hide some parts or present the information in a simpler way with simplified functionality. In this case it is absolutely OK to have one view model for those two views because otherwise you would have to duplicate most of the view model's code.

The MVVM pattern doesn't enforce the one-to-one relationship between view and view model, but in most cases it is the recommended approach. And if you want to have multiple views for one view model you should think carefully before going that path because you might end up with a view model which has half of its members used by one view and the other half by another.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
0

I actually used the fact that this one-to-one relationship is not enforced in a project.

We had an alerts vm, and we wanted to show them in a list on one of our views, but than also popup a notification when there's a new alarm and do it in the main screen. By setting the data context of the popup control to the alarms vm, we could easily achieve this functionality.

omni96
  • 41
  • 1
  • 10