title: How to get a resource from a merged ResourceDictionary in a code-behind file?
body: <p>I have a custom look-less control derived from Control class. It's template is defined in Generic.xaml file. Now I want to add some UI stuff to it (mostly brushes) and want to do that in separate resource dictionary, and then access those stuff from the code-behind feil (*.xaml.cs). 
  See bellow:
  Generic.xaml (fragment):</p>
  
  <pre><code>&lt;ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:PTE.Controls" xmlns:sys="clr-namespace:System;assembly=mscorlib"&gt;
  &lt;ResourceDictionary.MergedDictionaries&gt;
      &lt;ResourceDictionary Source="PTE.Controls;component/Resources/CategoriesColors.xaml"/&gt;
  &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;CornerRadius x:Key="elementCornerBorder" BottomLeft="2" BottomRight="2" TopLeft="2" TopRight="2"/&gt; 
  &lt;Style TargetType="{x:Type local:Element}"&gt;
      &lt;Setter Property="Canvas.ZIndex" Value="50"/&gt;
      &lt;Setter Property="MinWidth" Value="60"/&gt;
      &lt;Setter Property="Template"&gt;...
  </code></pre>
  
  <p>CategoriesColors.xaml (fragment):</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;LinearGradientBrush StartPoint="0,1" EndPoint="1,0" x:Key="categoryNobleGas"&gt;
      &lt;GradientStop Color="White" Offset="0"/&gt;
      &lt;GradientStop Color="RoyalBlue" Offset="0.5"/&gt;
      &lt;GradientStop Color="SteelBlue" Offset="1"/&gt;
  &lt;/LinearGradientBrush&gt;...
  </code></pre>
  
  <p>Server side part (fragment):</p>
  
  <pre><code>        private static void OnCategoryNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var categoryName = (CategoryName)e.NewValue;
          var element = (Element) d;
          switch (categoryName)
          {
              case CategoryName.NobleGas:
                  element.Background = (Brush)element.TryFindResource("categoryNobleGas");
                  break;
              case CategoryName.Halogen:
                  element.Background = (Brush)element.TryFindResource("categoryHalogen");
                  break;
              case CategoryName.OtherNonmetal:
                  element.Background = (Brush)element.TryFindResource("categoryOtherNonmetal");
                  break;
              case CategoryName.Metalloid:
  </code></pre>
  
  <p>It doesn't work. Basically TryFindResource method always returns null. Any ideas how to make these stuff work together?
  Thank you!</p>
  
  <p><strong>UPD:</strong> If I add following line in the constructor of the control it works: </p>
  
  <pre><code>this.Resources = Application.LoadComponent(new Uri("PTE.Controls;Component/Resources/CategoriesColors.xaml", UriKind.Relative)) as ResourceDictionary;
  </code></pre>
  
  <p>But first, it will duplicate dictionary (load new one every time) and consume considerable more memory. Second, I really want to do it in XAML.</p>