title: Custom resource dictionary inside ControlTemplate or DataTemplate
body: <p><strong>EDIT: This problem occurs when using the standard .NET ResourceDictionary as well and appears to be an issue with using resource dictionaries inside control or data templates.</strong></p>
  
  <p>I have a custom resource dictionary that follows a common approach to sharing resource instances.</p>
  
  <p><a href="http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/" rel="noreferrer">http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/</a> 
  <a href="http://www.wpftutorial.net/MergedDictionaryPerformance.html" rel="noreferrer">http://www.wpftutorial.net/MergedDictionaryPerformance.html</a></p>
  
  <pre><code>public class SharedResourceDictionary : ResourceDictionary
  {
      static readonly Dictionary&lt;Uri, WeakReference&lt;ResourceDictionary&gt;&gt; SharedDictionaries = new Dictionary&lt;Uri, WeakReference&lt;ResourceDictionary&gt;&gt;();
  
      Uri _sourceUri;
  
      public new Uri Source
      {
          get
          {
              // Behave like standard resource dictionary for IDE...
              if (VisualStudio.IsInDesignMode)
                  return base.Source;
  
              return this._sourceUri;
          }
          set
          {
              // Behave like standard resource dictionary for IDE...
              if (VisualStudio.IsInDesignMode)
              {
                  base.Source = value;
                  return;
              }
  
              this._sourceUri = value;
  
              WeakReference&lt;ResourceDictionary&gt; cached;
              if (SharedDictionaries.TryGetValue(value, out cached))
              {
                  ResourceDictionary rd;
                  if (cached.TryGetTarget(out rd))
                  {
                      this.MergedDictionaries.Add(rd);
                      return;
                  }
              }
  
              base.Source = value;
              SharedDictionaries[value] = new WeakReference&lt;ResourceDictionary&gt;(this);
          }
      }
  }
  </code></pre>
  
  <p>It works fine, but whenever it is referenced inside a Resources element within a ControlTemplate or DataTemplate, there are spurious errors shown (these do not affect the build, which still succeeds).</p>
  
  <p>This one gets shown for the standard ResourceDictionary which contains SharedResourceDictionary in its merged dictionaries:</p>
  
  <pre><code>Unable to cast object of type 'Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode' to type 'System.Windows.ResourceDictionary'
  </code></pre>
  
  <p>Sample XAML:</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type vm:MyViewModel}"&gt;
      &lt;DockPanel Style="{DynamicResource MainDockPanel}"&gt;
          &lt;DockPanel.Resources&gt;
              &lt;ResourceDictionary&gt;
                  &lt;ResourceDictionary.MergedDictionaries&gt;
                      &lt;p:SharedResourceDictionary Source="/MyAssembly;component/MyResources.xaml" /&gt;
                  &lt;/ResourceDictionary.MergedDictionaries&gt;
              &lt;/ResourceDictionary&gt;
          &lt;/DockPanel.Resources&gt;
      &lt;/DockPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Does anyone have any ideas how I can eliminate this nuisance error?</p>
  
  <p>Thanks</p>