Laurent has encouraged me to speak out about my MVVM handicap ;). I am slowly coming to up to speed with MVVM (currently in SL) and was wondering everyone's opinion on if it is good, bad or ugly to have multiple ViewModels in a single view.
-Jer
I agree with Mike opinion, when UI requires multiple ViewModels I use a ViewModel that exposes a series of sub-ViewModels that are databound to UI sub-areas (e.g.UserControls etc…) and coordinates sub-viewmodels.
This allows me to cleanly separate roles and results in a better testing.
Corrado
+1 on the single view model with exposed ( bindable and notifiable) properties exposing sub-Viewmodels..
for me it comes to DataContext … {there is only one inherited dataContext in the tree } … so I have one ViewModel and expose submodels from there..
Example, when I have a page called “Create Customer Profile” which has 3 parts ( login credentials, address, preferences ) … I still have a single viewmodel that exposes it all.. If the parts are usercontrols, I might pass just the “subModel” to the UserControl..
Extending this thought and maybe going towards Jer’s
question on multiple models, the one that I am still torn about is what I call “metadata”
… which is read-only data used to populate the view…
For example, imagine I have a real-estate app, and the UI shows a comboBox where
a user will choose their preference on rooms for the house (maybe list 1-10)..
I is data driven (comes from database), the SelectedItem is part of the ViewModel, no question there… but what about this ‘read-only meta-data (with the 1-10 list)?’… about half the time, I expose it via some {x:Static } and the other half of the time, I expose it off the viewmodel (again so it flows w/ datacontext).. How are others solving this??
10x,