title: WPF MVVM Why use ContentControl + DataTemplate Views rather than straight XAML Window Views?
body: <p><strong>Why This?</strong></p>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="MVVMProject.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Grid&gt;
          &lt;ContentControl Content="{Binding}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Have your ExampleView.xaml set up as:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vms="clr-namespace:MVVMProject.ViewModels"&gt;
      &lt;DataTemplate DataType="{x:Type vms:ExampleVM}" &gt;
          &lt;Grid&gt;
              &lt;ActualContent/&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>And create the window like this:</p>
  
  <pre><code>public partial class App : Application {
  
      protected override void OnStartup(StartupEventArgs e) {
  
          base.OnStartup(e);
  
          MainWindow app = new MainWindow();
          ExampleVM context = new ExampleVM();
          app.DataContext = context;
          app.Show();
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>When it can be done like this?</strong></p>
  
  <p>App.xaml: (Set startup window/View)</p>
  
  <pre><code>&lt;Application x:Class="MVVMProject.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      StartupUri="ExampleView.xaml"&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>ExampleView.xaml: (a Window not a ResourceDictionary)</p>
  
  <pre><code>&lt;Window x:Class="MVVMProject.ExampleView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vms="clr-namespace:MVVMProject.ViewModels"&gt;
      &gt;
      &lt;Window.DataContext&gt;
          &lt;vms:ExampleVM /&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;Grid&gt;
          &lt;ActualContent/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <hr>
  
  <p>Essentially it's <strong>"View as DataTemplate" (VaD) vs. "View as Window" (VaW)</strong></p>
  
  <p>Here is my understanding of the comparison: </p>
  
  <ul>
  <li>VaD: Lets you switch Views without closing the window. (This is not desirable for my project)</li>
  <li>VaD: VM knows absolutely nothing about the View, whereas in VaW it (only) has to be able to instantiate it when opening another window</li>
  <li>VaW: I can actually see my xaml rendered in the Designer (I can't
  with VaD, at least in my current setup)  </li>
  <li>VaW: Works intuitively with
  opening and closing windows; each window has (is) a corresponding View
  (and ViewModel)</li>
  <li>VaD: ViewModel can pass along initial window width, height, resizability etc. through properties (whereas in VaW they are directly set in the Window)</li>
  <li>VaW: Can set FocusManager.FocusedElement (not sure how in VaD)</li>
  <li>VaW: Less files, since my window types (e.g. Ribbon, Dialog) are incorporated into their Views</li>
  </ul>
  
  <hr>
  
  <p>So what's going on here?  Can't I just build my windows in XAML, access their data cleanly through properties of the VM, and be done with it? The code-behind is the same (virtually nil). </p>
  
  <p>I'm struggling to understand why I should shuffle all the View stuff into a ResourceDictionary.</p>