title: Override a background style programmatically
body: <p>I have a series of ToggleButtons that have a style applied to them. I want to be able to change the background colour when a button is checked. Each ToggleButton is assigned to the same Checked event which has the following code.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Sub togAnswer_Checked(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
      'togAnswer was clicked....
      Dim togC as ToggleButton
      Dim togS as String = TryCast(sender, ToggleButton).Name.ToString()
      'Cycle through all answers and set style
      Dim bc = New BrushConverter()
  
      For x = 1 to 3
          togC = DirectCast(FindName("togAnswer" &amp; x), ToggleButton) 
  
          If togS = "togAnswer" &amp; x.ToString Then
              'Set ToggleButton to selected colour
              togC.Background = DirectCast(bc.ConvertFrom("#AAFF8020"), Brush)
          Else    
              'Set other ToggleButtons to unselected colour
              togC.Background = DirectCast(bc.ConvertFrom("#AA000000"), Brush)
          End If
      Next
  End Sub
  </code></pre>
  
  <p>The code correctly identifies the source ToggleButton but does not change the color.</p>
  
  <p>The style assigned to the ToggleButtons is:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Style x:Key="GlassToggleButton" TargetType="{x:Type ToggleButton}"&gt;
      &lt;Setter Property="FontSize" Value="24" /&gt;
      &lt;Setter Property="Foreground" Value="White" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                  &lt;Border x:Name="ToggleBorder" 
                      CornerRadius="0 15 0 15" 
                      Background="#AA000000"  
                      BorderBrush="#99FFFFFF"
                      RenderTransformOrigin="0.5,0.5"&gt;
                      &lt;Grid&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="*"/&gt;
                              &lt;RowDefinition Height="1.7*"/&gt;
                          &lt;/Grid.RowDefinitions&gt;
                          &lt;Border Grid.Row="0" CornerRadius="0,14,0,0"&gt;
                              &lt;Border.Background&gt;
                                  &lt;LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"&gt;
                                      &lt;GradientStop Color="#02FFFFFF" Offset="0"/&gt;
                                      &lt;GradientStop Color="#55FFFFFF" Offset="0.5"/&gt;
                                      &lt;GradientStop Color="#11FFFFFF" Offset="1"/&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Border.Background&gt;
                          &lt;/Border&gt;
                          &lt;ContentPresenter x:Name="ToggleContentPresenter"
                              VerticalAlignment="Center"
                              HorizontalAlignment="Center"
                              Grid.RowSpan="2" /&gt;
                      &lt;/Grid&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsPressed" Value="True"&gt;
                          &lt;Setter Property="RenderTransform" TargetName="ToggleBorder"&gt;
                              &lt;Setter.Value&gt;
                                  &lt;TransformGroup&gt;
                                      &lt;ScaleTransform ScaleX="0.95" ScaleY="0.95"/&gt;
                                  &lt;/TransformGroup&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                          &lt;Setter Property="Background" Value="#AAFF8020" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsChecked" Value="True"&gt;
                          &lt;Setter Property="Background" Value="#AAFF8020" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsChecked" Value="False"&gt;
                          &lt;Setter Property="Background" Value="#AA000000" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsChecked" Value="{x:Null}"&gt;
                          &lt;Setter Property="Background" Value="#AA000000" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>