title: Programmatically Accessing a Silverlight Static Resource
body: <p>I'd like to programmatically access static resources much as I would in XAML:</p>
  
  <pre><code>&lt;TextBlock Text="{Binding Source={StaticResource My.Text.Key}}" /&gt;
  </code></pre>
  
  <p>This works whether my static resource is defined on the TextBlock, some parent element (e.g. UserControl) or even the application.  It seems that either the StaticResource binding expression knows how to walk up the element tree, or the element itself does.  I'd like to do the same thing programmatically:</p>
  
  <pre><code>&lt;UserControl x:Class="MyCustomControl" ...&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="Resources.xaml"/&gt; &lt;!-- Sets 'My.Text.Key' to System.String 'Hello, World!' --&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
  &lt;/UserControl&gt;
  
  public partial class MyCustomControl
  {
      public MyCustomControl()
      {
          InitializeComponent();
          string myCustomValue = this.Resources[MyCustomValue] as string; // myCustomValue becomes null!
      }
  }
  </code></pre>
  
  <p>Even in this simple test, my resource can't seem to be accessed programmatically.  And this is the simplified version of what I was trying to really do: find a static resource via an element that I have a custom dynamic property attached to (e.g. uiElement.Resources[key]).</p>