2
votes

I started playing around with Windows Phone development. I am using MVVM ligth and I am trying to follow good practices. But lately I ran into an issue: Is it okay to use ViewModel inside a ViewModel as a property?

My scenario is that I am having a MainViewModel which is a Panorama object. This Panorama objects consists of few PanaoramaItems (Notes, Calender and About Controls/Pages).

My question is, if I have for the MainPage.xaml a MainViewModel, is it okay if I put the other ViewModels as properties (NotesViewModel, CalenderViewModel, AboutViewModel) to the MainViewModel, in that case those will inherit from the MainPage the DataContext and I would just bind to a property of the MainViewModel. Or should I rather use the locator pattern to allow the other pages/control to get their own ViewModels and do not inherit the DataContext?

Is it okay, if a control has a ViewModel or should it be rather for pages?

1

1 Answers

3
votes

If the Parent and Child ViewModels are related: sure, that is fine, it does not violate the pattern.

This setup allows you to re-use ViewModels and Views across pages and controls.