title: WPF Change Thickness object in Resource and assign in code behind
body: <p>I have defined one Thickness resource in Window's resources collection which is set to value 10 along all sides.  I have 3 Buttons in that window.  </p>
  
  <p>Upon clicking the third button, I am fetching the value of that resource, changing it(200, all edges) and applying it statically for first button and dynamically for second but still it's picking up the old value(10) for the button which is using it dynamically.  For Buttton using it statically it was supposed to fetch the old value (10) but I thought just because the second button is fetching it dynamically, it will reflect the change(200).  </p>
  
  <pre><code>&lt;Window x:Class="WpfApplicationUnleashed.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
          Title="Window1" &gt;
  
  
      &lt;Window.Resources&gt;
          &lt;Thickness x:Key="BadiThickness"&gt;10&lt;/Thickness&gt;
      &lt;/Window.Resources&gt;
  
      &lt;StackPanel&gt;
          &lt;Button x:Name="cmdStatic" HorizontalAlignment="Center" &gt;
              I am Static
          &lt;/Button
  
          &lt;Button x:Name="cmdDynamic" HorizontalAlignment="Center" &gt;
              I am Dynamic 
          &lt;/Button&gt;
  
          &lt;Button x:Name="cmdChanger" HorizontalAlignment="Center" Click="cmdChanger_Click"&gt;
   I am Changer
          &lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>private void cmdChanger_Click(object sender, RoutedEventArgs e)
  {
      Thickness th = (Thickness)this.FindResource("BadiThickness");
      th.Bottom = 200;
      th.Top = 200;
      th.Left = 200;
      th.Right = 200;
  
      cmdDynamic.SetResourceReference(Button.MarginProperty, "BadiThickness");
      cmdStatic.Margin = (Thickness)this.FindResource("BadiThickness");
  }
  </code></pre>