title: Changing the styles at runtime in WPF
body: <p>I am trying to allow the user to customize the elements in a WPF application. What I am trying to achieve is, if I have a list box which specifies all the form elements (TextBox, label etc.) user can pick one form element, and set the style property say Label, foreground should be in orange where as for TextBox foreground should be in black and so on. And as per what ever style I am intending to apply all the TextBoxes should look alike.</p>
  
  <p>I am not able to go figure out a way for achieving this. I have tried out an example where multiple pre-defined styles can be uploaded at runtime. So now, I would like to find a way of changing the property of different elements at runtime.</p>
  
  <p><strong><code>UPDATE:</code></strong></p>
  
  <p>I tried to create a new style from the code behind.</p>
  
  <p><code>XAML</code></p>
  
  <pre><code>&lt;Label Content="SAMPLE" Style="{DynamicResource Style1}" x:Name="label1" /&gt;
  &lt;Button Content="Button" Click="Button_Click" /&gt;
  </code></pre>
  
  <p>and in code behind i.e. on click of the Button I tried this:</p>
  
  <pre><code>Style style = new Style { TargetType = typeof(Label) };
  style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Black));
  Application.Current.Resources["Style1"] = style;
  </code></pre>
  
  <p>But it is not getting updated.</p>
  
  <p>Thanks.</p>