title: Implementing a view-model-first approach inside a parent view/view model using MEFedMVVM
body: <p>I am writing a WPF application using MEF and a third-party library called <a href="http://mefedmvvm.codeplex.com" rel="nofollow">MEFedMVVM</a>.</p>
  
  <p>I am attempting to create a design whereby a parent view model has a collection of child view models, and I wish to use the view-model-first approach as this keeps the views outside of the view models thereby keeping the code more view model-centric and more unit testable.</p>
  
  <p>I have read <a href="http://mefedmvvm.codeplex.com/discussions/270277" rel="nofollow">this discussion</a> and <a href="http://mefedmvvm.codeplex.com/discussions/300184" rel="nofollow">this discussion</a> regarding using DataTemplate for the view, and also Reed Copsy, Jr's suggestion <a href="http://social.msdn.microsoft.com/Forums/da/wpf/thread/a1ac2e19-9137-417d-8144-6562e51f5a09" rel="nofollow">here</a> to use a generic view to view model mapping resource.  But, I'm struggling to actually implement something that works.</p>
  
  <p>My parent view is very simple:</p>
  
  <pre><code>&lt;UserControl x:Class="MyParentView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
               meffed:ViewModelLocator.ViewModel="MyParentViewModel" /&gt;
  </code></pre>
  
  <p>The parent view model derives from a base type that implements <em>IContextAware</em>:</p>
  
  <pre><code>[ExportViewModel("MyParentViewModel")]
  public class MyParentViewModel : ViewModelBase
  {
      [ImportingConstructor]
      public MyParentViewModel()
      {
          var myChildVM = ServiceLocator.Current.GetInstance&lt;MyChildViewModel&gt;());
      }
  }
  </code></pre>
  
  <p>This is the child view model:</p>
  
  <pre><code>[Export(typeof(MyChildViewModel))]
  [ExportViewModel("MyChildViewModel", true)]
  public class MyChildViewModel : ViewModelBase
  {
  }
  </code></pre>
  
  <p>And this has a corresponding view:</p>
  
  <pre><code>&lt;UserControl x:Class="MyChildView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
               meffed:ViewModelLocator.ViewModel="MyChildViewModel" /&gt;
  </code></pre>
  
  <p>Initially, I thought that specifying the second Boolean parameter on <em>ExportViewModel</em> attribute for <em>MyChildViewModel</em> would make everything work using a view-model-first approach as my views and view models are MEFed together in the views' XAML code.  However, turns out this is not the case, and what I actually get passed in to <strong>IContextAware.InjectContext()</strong> when I instantiate a <em>MyChildViewModel</em> object in the <em>MyParentViewModel</em> constructor is a <em>MyParentView</em> object.  Not a <em>MyChildView</em> object as I was expecting and hoping.  Clearly, I need to add something to wire them up together.  Could anyone provide an example on how to do this?</p>
  
  <p>Thanks!</p>