I like Igorโs suggestion, but without a viewmodel that knows the point of view. I prefer my dependencies to go in one direction (View -> ViewModel -> Model).
What I am doing is ViewModel-First and just a DataTemplate in view mode. So I do this:
MainViewModel mainViewModel = container.Resolve<MainViewModel>(); region.Add(mainViewModel, "MainView"); region.Activate(mainViewModel);
With the addition of the ViewModel -> View view made using the WPF dataset (I don't think this approach is possible with Silverlight, though)
App.xaml:
<Application.Resources> <DataTemplate DataType="{x:Type viewModels:MainViewModel}"> <views:MainView /> </DataTemplate> </Application.Resources>
What is it! I like this approach. I love the way it looks like magic. It also has the following advantages:
- No need to change the constructors to match the mapping
- No need to register a type for IMyViewModel in a container ... you can work with specific types. I like to register with services like IViewRegistry or ILogger ... things like that
- You can change the display using resources tied to a specific view in which the region is located (this is good if you want to reuse ViewModels, but want them to look different in different areas of the application.
source share