title: SilverLight Control Library - Cannot Reference Relative MergedDictionary by Generic.xaml
body: <p>I'm at my ropes end with this. I have spent countless hours trying to figure this out and no such luck.</p>
  
  <p><strong>Short Explanation of Problem</strong></p>
  
  <p>Inside my custom control class, when I check Application.Current.Resources["key"] I am returned null. This "Key" style is inside a local dictionary which is supposed to be merged with the Application.Current.Resources by my Control Library's themes/generic.xaml resource.</p>
  
  <p>How do I reference/confirm a reference to a MergedDictionary in my SilverLight Control Library's themes/generic.xaml.</p>
  
  <p>Is this even possible or is my thinking on how merged resources are suppose to be merged entirely wrong?</p>
  
  <p>Please help. Thanks in advance.</p>
  
  <p><strong>Long Explanation of Problem</strong></p>
  
  <p>I have a Silverlight Control Library with a Controls folder, and a Themes folder. Inside the Themes folder I have generic.xaml. Its content:</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;ResourceDictionary.MergedDictionaries&gt;
          &lt;ResourceDictionary Source="/SilverLightLib;component/Themes/EnhancedLabelDict.xaml" /&gt;
      &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Inside the Themes folder I have EnhancedLabelDict.xaml. Its content:</p>
  
  <pre><code>&lt;Style x:Key="ReadOnlyTextBox" TargetType="TextBox"&gt;
          &lt;Setter Property="BorderThickness" Value="1"/&gt;
          &lt;Setter Property="Background" Value="#FFFFFFFF"/&gt;
          &lt;Setter Property="Foreground" Value="#FF000000"/&gt;
          &lt;Setter Property="Padding" Value="2"/&gt;
          &lt;Setter Property="BorderBrush"&gt;
          &lt;!-- A lot more code --&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Both these files build action is set to Page.</p>
  
  <p>Now I have no idea if the generic.xaml is even loading my resource. The only way I can tell is if I put some un-formatted text between . This causes an error.</p>
  
  <p>If I use an incorrect path to my ResourceDictionary, I receive a run time error - 'Failed to assign to property 'System.Windows.ResourceDictionary.Source'</p>
  
  <p>Inside my Controls folder, I have EnhancedLabel.cs which extends ContentControl. Inside it's constructor, I create a new TextBox and assign it's style like so:</p>
  
  <pre><code>Style style = Application.Current.Resources["ReadOnlyTextBox"] as Style;
  this.textBox.Style = style;
  </code></pre>
  
  <p>I have this style in both the App.xaml and my EnhancedLabelDict.xaml which is inside my library. When I comment out the Style in App.xaml, the 'ReadOnlyTextBox' style is not found (null). Uncomment it, it is found.</p>
  
  <p>I don't understand why I cannot reference my style from within my EnhancedLabel.cs.</p>
  
  <p>If I take the EnhancedLabelDict.xaml, add it to a Themes folder inside a Resources folder inside my main Application. If I then add the following to my App.xaml:</p>
  
  <pre><code>&lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="/SilverLightPOC;component/Resources/Themes/EnhancedLabelDict.xaml" /&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>My control works! So other than the path, there is nothing different. But this doesn't work because I don't want to have to store Dictionary files that my Library depends on, inside the main application.</p>
  
  <p>Please help.</p>