Using the Prism v5.0 ViewModelLocater

Prism documentation is thorough. However, that makes it lack a certain conciseness that makes it easily digestible. One key thing that you will want to understand is how to get your views and view models connected. This post will quickly detail how to make it all work.

View Model Locator Configuration If your view model has a default constructor, it will be instantiated and matched to the view automatically. If your view models do anything interesting, they will likely not have default constructors. You will need to configure the view model locater to resolve constructor dependencies from your IOC container.

ViewModelLocationProvider.SetDefaultViewModelFactory(
    t => container.Resolve(t)
);

View to view model wiring is based on convention. They are match based on name, much like the familiar MVC conventions. Be careful though, if your views are suffixed with “view”, it will not work.

✅ Do this ViewModels.MyScenicViewModel – Views.MyScenic ❌ Don't do this ViewModels.MyScenicViewModel – Views.MyScenicView

The convention can be overridden with the following configuration point.

ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(
(viewType) => {
    ...
    return viewModelType;
});

View Specific Wire up Your view must implement the IView marker interface found in the Microsoft.Practices.Prism.Mvvm namespace.

public partial class Wizard : Window, IView {
    public Wizard() {
        InitializeComponent();
    }
}

And finally don't forget to set the auto wire up flag to true in your view.

<Window x:Class="MyProject.Views.Wizard"   
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:mvvm="clr-namespace:Microsoft.Practices.Prism.Mvvm;
 assembly=Microsoft.Practices.Prism.Mvvm.Desktop"
 Title="Wizard" Height="600" Width="800"
 mvvm:ViewModelLocator.AutoWireViewModel="True">

For a more in-depth read, check the Prism 5.0 Developers Guide.