title: How to animate ToggleButton background color in WPF (xaml)?
body: <p>I would like to dynamically change background color of <code>ToggleButton</code> depending on it's state. Here's my XAML code: </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ToggleButton
      Width="286"
      Height="50"
      x:Name="HistoryToggle"
      IsChecked="{Binding HistoryMode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
      IsEnabled="True"&gt;
      &lt;ToggleButton.Style&gt;
          &lt;Style TargetType="{x:Type ToggleButton}"&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                          &lt;Grid
                              x:Name="outerGrid"&gt;
                              &lt;Border
                                  x:Name="HistoryToggleBorder"                         
                                  BorderThickness="0"
                                  CornerRadius="4"
                                  Background="#1E3555"&gt;
                                  &lt;TextBlock
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      FontSize="20"
                                      FontWeight="Regular"
                                      Foreground="White"
                                      Text="MyToggle" /&gt;
                              &lt;/Border&gt;
                          &lt;/Grid&gt;
                          &lt;ControlTemplate.Triggers&gt;
  
                              &lt;EventTrigger 
                                  RoutedEvent="ToggleButton.Checked"&gt;
                                  &lt;BeginStoryboard&gt;
                                      &lt;Storyboard&gt;
                                          &lt;ColorAnimation
  
                                              Storyboard.TargetName="HistoryToggleBorder"
                                              Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                              From="#1E3555"
                                              To="#009ABE"
                                              Duration="0:0:0.15"
                                              FillBehavior="HoldEnd"/&gt;
  
                                      &lt;/Storyboard&gt;
                                  &lt;/BeginStoryboard&gt;
                              &lt;/EventTrigger&gt;
  
                              &lt;EventTrigger 
                                  RoutedEvent="ToggleButton.Unchecked"&gt;
                                  &lt;BeginStoryboard&gt;
                                      &lt;Storyboard&gt;
                                          &lt;ColorAnimation
  
                                              Storyboard.TargetName="HistoryToggleBorder"
                                              Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                              From="#009ABE"
                                              To="#1E3555"
                                              Duration="0:0:0.15"
                                              FillBehavior="HoldEnd"/&gt;
  
                                      &lt;/Storyboard&gt;
                                  &lt;/BeginStoryboard&gt;
                              &lt;/EventTrigger&gt;
  
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/ToggleButton.Style&gt;
  &lt;/ToggleButton&gt;
  </code></pre>
  
  <p>When the application tries to open window containing my <code>ToggleButton</code> an exception is thrown:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Name „HistoryToggleBorder” cannot
    be found in namespace „System.Windows.Controls.ControlTemplate”.'</p>
  </blockquote>
  
  <p>and the application crashes. What am I doing wrong?</p>