Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVVM there can be only one View for each one View Model?

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 ?

like image 645
user310291 Avatar asked Apr 09 '11 19:04

user310291


People also ask

Can a ViewModel have multiple views?

While a view should only have one viewmodel, a single viewmodel might be used by multiple views (imagine a wizard, for example, that has three views but all bind to the same viewmodel that drives the process).

Can we use single ViewModel for multiple activity?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel.

What does view model do in MVVM?

The view model implements properties and commands to which the view can data bind to, and notifies the view of any state changes through change notification events.

Can a view have multiple Viewmodels in WPF?

Err no - it's just convention that you have one instance, but there actually aren't any rules that you can't have multiple VM's in the XAML - the key thing to understand is that every item has it's own DataContext (that's inherited), so if you need to use a different DataContext then you just bind to the appropriate VM ...


2 Answers

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.

like image 64
StellarEleven Avatar answered Oct 02 '22 15:10

StellarEleven


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.

like image 27
pchajer Avatar answered Oct 02 '22 16:10

pchajer