How to use MVVM and MEF with DataTemplate?

Feb 11, 2010 at 5:52 PM

I am trying to use MEF on a new project within my application. Things work at the main app level, but in this separate project whose assembly is included in the main app (its a UserControl) I am wondering about something... If I have a UserControl, and in it I have the following:

  <UserControl.Resources>
     
<DataTemplate DataType="{x:Type vm:MyViewModel}" >
         
<local:MyView />
     
</DataTemplate>
 
</UserControl.Resources>
 
<vm:MyViewModel />

In my MyViewModel, I Export some stuff, and the assemblies seem to be composed just fine (Im using the CompositionInfoTextFormatter to check this). But my VM is not instantiated by MEF, or at least the constructor never gets called. And when it does get called it appears to be from the WPF framework, not MEF.

What is the right way to A) In xaml, associate my VM with its view with MEF (if different from above) and B) How do I instantiate my VM so that MEF controls it, and therefore the Exports and Imports work?

Currently they dont, which is why Im trying to make sense of this all :)