Prism+MEF: using TreeView through Prism & MEF Framework

Topics: Prism v4 - WPF 4
Apr 6, 2014 at 11:32 AM
Edited Apr 6, 2014 at 12:45 PM
I've been looking around forums but haven't found any satisfying help about using Treeview control within context of MVVM + Prism + MEF.
The most close thing was Josh Smith Article where he implemented Treeview control using ViewModel pattern as per this article at CodeProject .
My concern is to impose the Prism+MEF framework on this example :
1-How to create all the ViewModel Wrapper classes via MEF
2-How each ViewModel can Load & Populate the Child ViewModel using MEF and w/o using [new] operator

In short how to build Hierarchical ViewModels using Prism+MEF?. It would be very generous if you could build the same example using Prism+MEF as it would be a standard way to using such very important and hierarchical control in our Prism based applications.
Thanks for your valuable support.
Apr 11, 2014 at 2:59 PM
If possible I need to implement the following two classes using MEF and I will follow up for rest of the sample. How to handle the Constructors and LoadChildren() method in terms of MEF. I asked the same question in many forums but no answer!!!

public class CountryViewModel
readonly ReadOnlyCollection<RegionViewModel> _regions;
public ReadOnlyCollection<RegionViewModel> Regions
get { return _regions; }
public CountryViewModel(Region[] regions)
_regions = new ReadOnlyCollection<RegionViewModel>(
(from region in regions
select new RegionViewModel(region))

public class RegionViewModel : TreeViewItemViewModel
readonly Region _region;
public RegionViewModel(Region region)
: base(null, true)
_region = region;
public string RegionName
get { return _region.RegionName; }
protected override void LoadChildren()
foreach (State state in Database.GetStates(_region))
base.Children.Add(new StateViewModel(state, this));
Apr 14, 2014 at 7:11 PM

I want to apologize for the late response to your question. I´ve been investigating how to accomplish the scenario you are asking about.

As commented in the StackOverflow discussion related to this same problem (Unable to build Hierarchical View Models using MEF), you would need to register the ViewModel classes in the container, taking into account if the ViewModel needs to be registered as Singleton or not. In order to do this you would use Export attributes.

In addition, constructors would be imported from the container when instances are resolved. Therefore any argument of the constructors should be imported from the container as well. And this feature would need to perform some changes on the solution you provided. For example, the Region list parameter may need to be registered in the container, inside a RegionList class that contains te list. I will come back to you when I manage to resolve those dependencies.

The Prism Guide may be helpful for finding the way on configuring the MefBootstrapper and register the object types on it.

Gabriel Ostrowsky
Apr 14, 2014 at 8:23 PM
Hi Gabriel,
I'm glad that you follow up my question, and I wish you could figure out an answer because it is very important to be able to build hierarchy like Treeview using the powerful DI IOC MEF.
But the core problem that I am facing is that we can't pass parameters to constructor while composing parts using MEF which is the base to build a hierarchy.
I hope you can find out a way around this obstacle and I think this capability must be added to MEF
Good Luck.