title: How to move Content Control DataTemplate to Dictionary Resources XML
body: <p>I've been given a great example of how to apply a dynamic style within a Conten Control.</p>
  
  <p>I know my XAML for each style is going to get really big, I'd like to split the DataTemplate off to separate XAML files (I guess ResourceDictionary's). I just can't seem to get it to work.</p>
  
  <pre><code>&lt;ContentControl Content="{Binding MyViewModel}"&gt; 
  
  &lt;ContentControl.Resources&gt; 
      &lt;DataTemplate x:Key="DefaultTemplate"&gt; 
          &lt;TextBlock Text="DefaultTemplate" /&gt; 
      &lt;/DataTemplate&gt; 
  
      &lt;DataTemplate x:Key="TemplateA"&gt; 
          &lt;TextBlock Text="Template A" /&gt; 
      &lt;/DataTemplate&gt;  
  
      &lt;DataTemplate x:Key="TemplateB"&gt; 
          &lt;TextBlock Text="Template B" /&gt; 
      &lt;/DataTemplate&gt; 
  &lt;/ContentControl.Resources&gt; 
  
   &lt;ContentControl.Style&gt; 
       &lt;Style TargetType="{x:Type ContentControl}"&gt; 
           &lt;Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" /&gt; 
           &lt;Style.Triggers&gt; 
               &lt;DataTrigger Binding="{Binding SelectedView}" Value="ViewA"&gt; 
                   &lt;Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" /&gt; 
               &lt;/DataTrigger&gt; 
               &lt;DataTrigger Binding="{Binding SelectedView}" Value="ViewB"&gt; 
                   &lt;Setter Property="ContentTemplate" Value="{StaticResource TemplateB}" /&gt; 
               &lt;/DataTrigger&gt; 
           &lt;/Style.Triggers&gt; 
       &lt;/Style&gt; 
   &lt;/ContentControl.Style&gt; 
  </code></pre>
  
  <p></p>
  
  <p>As an example, I want this template:</p>
  
  <pre><code>    &lt;DataTemplate x:Key="TemplateB"&gt; 
          &lt;TextBlock Text="Template B" /&gt; 
      &lt;/DataTemplate&gt;  
  </code></pre>
  
  <p>to reside in a different XAML file (lets call it Test.xaml).</p>
  
  <p>I've tried creating a ResourceDictionary file and referencing the Key in the DataTemplate, but obviously it couldn't find the key because it resides in a different XAML file, so I've tried to implement a Merged ResourceDictionary but VS2010 throws errors - e.g.</p>
  
  <p>Test.xaml</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
  &lt;DataTemplate x:Key="TemplateB"&gt;
      &lt;TextBlock Text="Template B" /&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>with:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="Test.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>But then I get an error "Resources Property has already been set".</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Ben</p>