MVVM and DataTemplates from an unknown module.

Topics: Prism v4 - WPF 4
Jan 23, 2011 at 3:04 AM
Edited Jan 23, 2011 at 3:05 AM

Probably a confusing title for this question, but it's the best description I could come up with.  Here's an example of what I am asking about:

I'm creating an option box service for my application.  If you go Tools\Options in Visual Studio, what I'm creating looks just like that.  TreeView on the left, settings for the selected item on the right.

I have a Hierarchical class of OptionItems, I just use a HierarchicalDataTemplate to show them in the TreeView, and each item has a content property of type object.  I bind the content control on the right to the selected item of the TreeView and then use DataTemplates to format the content.  (The OptionItems and Content are ViewModels, the DataTemplates are the View.)

That's basically the same Technique as is used in the MVVM Quick Start for hierarchical ViewModels.

However... in this case, the OptionItem may be originating from some AddOn module that my Options Dialog Window should know nothing about.  So I can't create the template in the Dialog Window.

I've been hacking around it by setting content to a View instead of a ViewModel.  But that violates the MVVM pattern and the Prism guidance.

How would you implement this?

Jan 24, 2011 at 8:41 PM


Based on my understanding of your scenario, one possible way to achieve your requirement could be to use a shared service (you can read more about Shared Services in Chapter 9: Communicating Between Loosely Coupled Components) that holds an observable collection of OptionItems, and make your OptionItemsViewModel consume that collection and expose it as a property for your view to access it through Data Binding.

As explained in the aforementioned chapter, this instance should be a singleton one. You can achieve this by simply decorating your service with the PartCreationPolicyAttribute attribute set to Shared. This specifies that a single shared instance of the associated ComposablePart will be created by the CompositionContainer and shared by all requestors. Your modules could then retrieve an instance of your service through the dependency injection mechanism you are using, and add and remove OptionItems from the service’s collection.

As an example, you could take a look at the following sample, which illustrates the aforementioned approach here

You could also find the handy the In The Box MVVM Training by Karl Shifflett.

I hope you find this information useful.


Miguel Bronzovic

Jan 25, 2011 at 4:59 AM

Thanks Miguel,

I've actually written 2 prism MEF services already, so that should be pretty easy.  

Part of my problem may be that I'm still struggling a bit with the Region and Module aspects of prism.  I get MVVM pretty well, but I'm new to MEF and prisms modular design concepts.  I think part of my solution here may exist in the RegionManager, what I'm trying to avoid here is instantiating the View in the ViewModel. 

I'll review the links you gave and report back on this.

Jan 25, 2011 at 6:34 PM

In looking at your references, that seems similar to what I already have in the works.

I have a base service that collects the OptionsItems from each of the modules.  The OptionsItems are HierarchicalViewModels so they form the tree for the TreeView.  Since they are all based on a common base class I just use a HierarchicalDataTemplate and the TreeView draws itself.

Each OptionsItems has a content property that contains the ViewModel for the right hand content control.

The problem is instantiating the correct View, for the selected OptionsItem.Content property.

I have a few ideas I'm mulling over at the moment.

1. Create a view dictionary in the service.  The key would be the type of the ViewModel and the value would be a delegate that creates an instance of the correct view for the view model.  Then use the region manager to inject that view into the dialog. 

2. Create a custom RegionBehavior that instantiates the view based on the bound ViewModel. 

Does it sound like I'm on the right track?  (Have a better idea?)

Jan 26, 2011 at 4:09 PM
Edited Jan 26, 2011 at 4:20 PM


I wouldn´t recommend to put the ViewModel for your views inside the OptionItems' content property. One alternative approach would be to register the ViewModels in Mef using a contract name, and store a string containing that name in your OptionItems' content property. Then, when you click that OptionItem, you could navigate to that view in the right region, passing that string as the Uri. Note that you can also pass parameters to the navigation request.

You can read more about Navigation in this chapter from the Prism documentation. You could also find the View-Swtiching Navigation QuickStart and the Navigation HOL in the Prism Training Kit useful.

Therefore, you don’t need to implement neither a dictionary nor a behavior to instantiate the correct view based on the selected item on the tree.

Once again, please let me know if this helps you.


Miguel Bronzovic


Jan 26, 2011 at 5:45 PM

I was looking at those yesterday, I also found "Prism v4 Region Navigation Pipeline" really helpful, once I had looked at those.

I think I need to play with my model next and ensure that I have a decent XPath syntax.

I had kind of abandoned the idea of going this route, but I think your right, this might be the best route, I'll give it a shot.

Thanks again for your help.  I'll let you know how it turns out, or if I have more questions.



Jan 30, 2011 at 3:19 AM

So... in case anyone comes across this thread, here's what I wound up with.

To add items to the options dialog, a module creates a class or classes that export IOptionsProvider.

IOptionsProvider is very simplistic it only requires a key to uniquely identify itself, A method that returns the option items for the tree view, and a method to get the data for an option item based on a key.

I have an options service that is responsible for showing the dialog.  It looks for any modules that export IOptionsProvider and builds the hierarchical view model for the tree view using the get option items method of the IOptionsProvider.

The option items contain an exported key for the View that will display on the right, and a provider and data key for it's data.

When the selection in the tree view changes normal region navigation is called using a URI built from the option item.  It took a bit to get that working the solution is in this thread.

The ViewModel for the right hand control that is navigated to, makes a call to the options service, using the keys in the URI to obtain the data it needs. It all seems to be working great so far.  And the simplicity of just adding an IOptionsProvider class to a module in order to implement this will be nice!

Thanks for your help on this Miguel, I think I like the way it turned out.