One pattern that MVVM Light uses is a static ViewModelLocator class that holds all of the main/root view models in the application. You declare it as a data source in the app.xaml:
<vm:ViewModelLocator d:isdatasource="True" x:key="Locator"/>
Then in the page Xaml you can do:
DataContext="{Binding Main, Source={StaticResource Locator}}"
This is great when there is basically a 1:1 mapping between the page and the contents of each view model. What it's not so good at is dealing with the situation where you want the same page to bind to multiple view models are structurally equivalent but have different data contents.
Take for instance an RSS viewer (which coincidentally enough I'm working on at the moment). You might have a feed that represents articles form a website and another feed that is discussion posts from the same website. Each feed is further broken down into topic channels. So if we wanted to display each feed as a page, with each topic as a Pivot Item on that page we could distill the page xaml down to:
<controls:Pivot Title="{Binding Name}" ItemsSource="{Binding Topics}" ItemTemplate="{StaticResource RssTopicTemplate}"/>Now if the ViewModel is statically linked to the page (as above) we need to parametrize the ViewModel as the user navigates from articles to discussions and back. It can be made to work but it violates the Single Responsibility Principle and I just don't like it.
So rather than parameterizing the ViewModel how about we parametrize the page? Let's declare the linkage between ViewModel in page on the ViewModel. We'll declare an attribute that specifies the linkage and allows a parameter to be passed the page in the form of a query string.
[Page("/RssPage.xaml?vm=ArticlesStatic")] public class ArticlesViewModel : ViewModelBase ... [Page("/RssPage.xaml?vm=ForumsStatic")] public class ForumsViewModel : ViewModelBase
In this app the set of navigable items are collections of viewmodels that are displayed in ListBoxes wherein each ViewModel can be selected and navigated to:
public class ContentsViewModel : ViewModelBase { public ObservableCollection<ViewModelBase> Contents { get; private set; } public RelayCommand<object> SelectViewModel { get; private set; } private void Select(object vm) { if (vm != null) { var page = vm.GetType().GetAttribute<PageAttribute>(); Navigate(page.Page); } } }Then we need a wee bit of code in the page codebehind:
protected override void OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.ContainsKey("vm")) { string key = NavigationContext.QueryString["vm"]; DataContext = ViewModelLocator.FindViewModel(key); } base.OnNavigatedTo(e); }where FindViewModel is a method added to the ViewModelLocator that returns the correct ViewModel using reflection:
public static object FindViewModel(string key) { var prop = typeof(ViewModelLocator).GetProperty(key, BindingFlags.Public | BindingFlags.Static); return prop.GetValue(null, null); }
I find that moving the linkage between View and ViewModel onto the ViewModel gives us the flexibility to reuse the same UI to display the contents of multiple, structurally equivalent ViewModels, while still maintaining a loose coupling between those two layers.
CodeProject