title: How to bundle View, ViewModel and DataTemplate in a WPF application for easy reuse?
body: <h1>Situation:</h1>
  
  <p>I'd like to create a flexible application which is ViewModel driven.</p>
  
  <p>The basic flow is like this:</p>
  
  <ol>
  <li>Design the main ViewModel</li>
  <li>Create a UserControl as View and a DataTemplate for the main ViewModel to select this View</li>
  <li>If there are sub components, the are modelled by sub ViewModels</li>
  <li>Create a UserControl as View and a DataTemplate for the sub ViewModel to select this View</li>
  </ol>
  
  <p>If a sub view model needs to be presented, it is done via a DataTemplate.</p>
  
  <p>This approach can also be seen <a href="http://paulstovell.com/blog/mvvm-instantiation-approaches" rel="noreferrer">here</a> (option 8).</p>
  
  <p>So the main window xaml looks something like this:</p>
  
  <pre><code>&lt;Window&gt;
      &lt;!-- somehow I need to add the mapping from ViewModel to View --&gt;
      &lt;Grid&gt;
          &lt;!-- the main ViewModel --&gt;
          &lt;ContentPresenter Content="{Binding Path=Content}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The <code>Content</code> property might contain a view model that contains a list of elements named <code>Children</code> and it's associated DataTemplate might look like this:
  The children are also flexibly rendered by a suitable DataTemplate.</p>
  
  <pre><code>&lt;UserControl&gt;
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;!-- display the child ViewModels  in a list --&gt;
              &lt;ItemsControl ItemsSource="{Binding Path=Children}" /&gt;
           &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <h1>Question:</h1>
  
  <ol>
  <li><p>How should I organize the ViewModels, Views and their DataTemplates so I don't need to hardwire them in the MainWindow?</p></li>
  <li><p>How do I then connect this to the main window? </p></li>
  <li><p>It would be nice if it is stub-able, i.e. I can see the result during design time with a design time dataContext.</p></li>
  </ol>
  
  <p>Basically I want to bundle the View, ViewModel and DataTemplate and be able to use them in an application that doesn't need to know about the details (e.g. some sub ViewModel implements a certain interface and is injected into the main ViewModel).</p>