title: Set static resource in code
body: <p>I have a few styles in my <code>App.xaml</code> file:</p>
  
  <pre><code>&lt;SolidColorBrush x:Key="styleBlue" Color="#FF4B77BE"/&gt;
  &lt;SolidColorBrush x:Key="styleRed" Color="#FFF64747"/&gt;
  &lt;SolidColorBrush x:Key="styleOrange" Color="#FFF89406"/&gt;
  &lt;SolidColorBrush x:Key="styleGreen" Color="#FF1BBC9B"/&gt;
  &lt;SolidColorBrush x:Key="styleYellow" Color="#FFF9BF3B"/&gt;
  
  &lt;Style x:Key="stackpanelBackground" TargetType="StackPanel"&gt;
      &lt;Setter Property="Background" Value="{StaticResource styleBlue}"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>I want to change the <code>BackgroundProperty</code> in the code of my <code>mainpage.xaml.cs</code>.</p>
  
  <p>I tried using this:</p>
  
  <pre><code>Style style = Application.Current.Resources["stackpanelBackground"] as Style;          
  style.Setters.SetValue(StackPanel.BackgroundProperty, "{StaticResource styleRed}");
  </code></pre>
  
  <p>But I get a catastrophic failure exception. I think it  has to do with <code>{StaticResource styleRed}</code>. Is there a better way to do this?</p>