Using Structuremap to resolve ViewModels

One of the things I find myself doing over and over again when working in xaml-based applications is to create a ViewModelFactory based on Jeremy D. Miller’s StructureMap.

All my ViewModel classes are based on a class BaseViewModel:

    public class BaseViewModel : INotifyPropertyChanged

The fun, of course, begins when your viewmodels need to know about other viewmodels. Lets say, for instance that your main view contains a Container control in which you would like to swap views in. Using structuremap, it is easy to inherit the Registry class in order to stitch together associations between views and models:

public class ViewModelsRegistry : Registry
    public ViewModelsRegistry()
        Rig<MainWindowView, MainVindowViewModel>(Registered.MainWindow);
        Rig<TrioEventsView, TrioEventsViewModel>(Registered.TrioEvents);
        Rig<NewsContentView, NewsContentViewModel>(Registered.NewsContent);

    private void Rig<TControl, TViewModel>(Registered name) 
        where  TControl : ContentControl where TViewModel : BaseViewModel

The Registered reference is an enum that I use to keep tabs on what views/viewmodels that are implemented:

    public enum Registered
        Unknown = 0,

As you can tell, the ViewModelsRegistry uses ContentControl as it’s container type. This makes it  compatible with all derivates, including Window and UserControl.
Following the factory pattern, the result follows:

public static class ViewModelFactory
    public static void Initialize(Registry registry)
        ObjectFactory.Configure(o => o.AddRegistry(registry));            

    public static ContentControl Get(Registered name)
        var control = ObjectFactory.GetNamedInstance<ContentControl>(name.ToString());
        control.Loaded += (s, e) => {
            control.DataContext = GetDataContextFor(name);
        return control;

    public static BaseViewModel GetDataContextFor(Registered name)
        return ObjectFactory.GetNamedInstance<BaseViewModel>(name.ToString());

The ViewModelFactory basically leans on structuremap providing the method Get() that stitches together a view and it’s model. One example of such use:


        public void LoadEvents(object dummy)

        private void SetViewModel(Registered name)
                _loadedControls.Add(name, ViewModelFactory.Get(name));
            CurrentContent = _loadedControls[name];

In the above snippet, taken from the MainWindowViewModel,  LoadEvents is bound to a button click on the Main Window. Once clicked, If I haven’t already loaded the control, I use the factory.Get() method to create a UserControl with initalized datacontext, before I finally set the ContentPresenter (CurrentContent) on the Main window viewmodel.

The factory can easilly be expanded to swap out datacontexts, provide different views. This blog post is mainly about using structuremap to make it happen.

Jeg bare tøyser


About digitaldias

Software Engineer during the day, photographer, videographer and gamer in the evening. Also a father of 3. Pedro has a strong passion for technology, and gladly shares his findings with enthusiasm.

View all posts by digitaldias →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.