title: WPF Button background action gets overwritten by the code
body: <p>I've got a button that I've modified its template so it shows different background colour when the button gets clicked and mouseover (it goes green rather than light blue).</p>
  
  <p>That works great by itself but I need to change the background colour according to some some state so I need to change the background in the code.</p>
  
  <p>My problem is when I change the background colour in the code, it changes correctly but it's no longer changing background for click or mouseover events.</p>
  
  <p>I'm assuming that I somehow overwritten the background colour in the code so what I created in the template is covered. 
  How can I fix this problem so I can change the background through the code but still has the action to click and mouseover?</p>
  
  <p>Here's my XAML code:</p>
  
  <pre><code>&lt;Style x:Key="NodeButtonStyle" TargetType="{x:Type Button}"&gt;
              &lt;Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/&gt;
              &lt;Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/&gt;
              &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/&gt;
              &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
              &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
              &lt;Setter Property="Padding" Value="1"/&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                          &lt;Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"  RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true" OverridesDefaultStyle="true"&gt;
                              &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt;
                          &lt;/Microsoft_Windows_Themes:ButtonChrome&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="IsKeyboardFocused" Value="true"&gt;
                                  &lt;Setter Property="Background"&gt;
                                      &lt;Setter.Value&gt;
                                          &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                                              &lt;GradientStop Color="#FFCFF9C4" Offset="0"/&gt;
                                              &lt;GradientStop Color="#FF249505" Offset="1"/&gt;
                                              &lt;GradientStop Color="#FF58D835" Offset="0.5"/&gt;
                                          &lt;/LinearGradientBrush&gt;
                                      &lt;/Setter.Value&gt;
                                  &lt;/Setter&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                  &lt;Setter Property="Foreground" Value="#ADADAD"/&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                                  &lt;Setter Property="Background"&gt;
                                      &lt;Setter.Value&gt;
                                          &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0" Opacity="0.5"&gt;
                                              &lt;GradientStop Color="#FFCFF9C4" Offset="0"/&gt;
                                              &lt;GradientStop Color="#FF249505" Offset="1"/&gt;
                                              &lt;GradientStop Color="#FF58D835" Offset="0.5"/&gt;
                                          &lt;/LinearGradientBrush&gt;
                                      &lt;/Setter.Value&gt;
                                  &lt;/Setter&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>In the code, I just do
  button1.Background = newBrush;</p>
  
  <p>I've tried to look for similar questions but I couldn't locate any..
  Any help would be appreciated! 
  I'm using blend 4 .net 3.5 and C#.</p>