title: Control template trigger not firing correctly after property change in code behind [WPF]
body: <p>I'm making a periodic table of elements wpf application and have made my buttons like this:</p>
  
  <pre><code>&lt;!-- RoundedButton.xaml --&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;Style x:Key="ButtonFocusVisual"&gt;
      &lt;Setter Property="Control.Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate&gt;
                  &lt;Border&gt;
                      &lt;Rectangle SnapsToDevicePixels="true" Margin="4" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="RoundedButton" TargetType="{x:Type Button}" x:Name="butt"&gt;
      &lt;Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/&gt;
      &lt;Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt;
      &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
      &lt;Setter Property="BorderThickness" Value="3"/&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="Padding" Value="0,0,1,1"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
  
  
                  &lt;Border CornerRadius="12,12,12,12" BorderThickness="3,3,3,3" RenderTransformOrigin="0.5,0.5" x:Name="border" BorderBrush="Transparent"&gt;
                      &lt;Border.RenderTransform&gt;
                          &lt;TransformGroup&gt;
                              &lt;ScaleTransform ScaleX="1" ScaleY="1"/&gt;
                              &lt;SkewTransform AngleX="0" AngleY="0"/&gt;
                              &lt;RotateTransform Angle="0"/&gt;
                              &lt;TranslateTransform X="0" Y="0"/&gt;
                          &lt;/TransformGroup&gt;
                      &lt;/Border.RenderTransform&gt;
  
                      &lt;Border Background="{TemplateBinding Background}" CornerRadius="12,12,12,12"  x:Name="bordertrue"&gt;
                          &lt;Grid&gt;
  
                              &lt;Border Opacity="0" x:Name="Shine" Width="Auto" Height="Auto" CornerRadius="12,12,10,10" Margin="0,0,0,0"&gt;
                                  &lt;Border.Background&gt;
                                      &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                          &lt;GradientStop Color="Indigo" Offset="0"/&gt;
                                          &lt;GradientStop Color="Crimson" Offset="0.5"/&gt;
                                          &lt;GradientStop Color="Crimson" Offset="0.5"/&gt;
                                          &lt;GradientStop Color="Indigo" Offset="1"/&gt;
                                      &lt;/LinearGradientBrush&gt;
                                  &lt;/Border.Background&gt;
                              &lt;/Border&gt;
                              &lt;ContentPresenter VerticalAlignment="Center"  Grid.RowSpan="2" HorizontalAlignment="Center" x:Name="contentPresenter"/&gt;
                          &lt;/Grid&gt;
                      &lt;/Border&gt;
                  &lt;/Border&gt;
  
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsEnabled" Value="False"&gt;
                          &lt;Setter Property="Opacity" TargetName="border" Value="1"/&gt;
                          &lt;Setter Property="Opacity" TargetName="contentPresenter" Value="0.5"/&gt;
                      &lt;/Trigger&gt;
  
                      &lt;Trigger Property="IsPressed" Value="True"&gt;
                          &lt;Setter Property="RenderTransform" TargetName="border"&gt;
                              &lt;Setter.Value&gt;
                                  &lt;TransformGroup&gt;
                                      &lt;ScaleTransform ScaleX="0.9" ScaleY="0.9"/&gt;
                                      &lt;SkewTransform AngleX="0" AngleY="0"/&gt;
                                      &lt;RotateTransform Angle="0"/&gt;
                                      &lt;TranslateTransform X="0" Y="0"/&gt;
                                  &lt;/TransformGroup&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                      &lt;/Trigger&gt;
  
                      &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                          &lt;Setter Property="Opacity" TargetName="Shine" Value="1"/&gt;
                          &lt;Setter Property="BorderBrush" TargetName="border" Value="Gold"/&gt;
                          &lt;Setter Property="Foreground"  Value="Gold" /&gt;
                          &lt;Setter Property="FontWeight"  Value="ExtraBold"/&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>And this is working perfectly on mouseover. However in my app I have a combination of textbox and listbox which makes me a searchbox. Whenever I search for a particular element I wish to make the same "Shine button" effect as on mouseover in style above</p>
  
  <p>I've tried to do it like this:</p>
  
  <pre><code>private void highlight(string elementName, bool MultiOrSingle)
      {
          LinearGradientBrush gradient1 = new LinearGradientBrush();
          gradient1.StartPoint = new Point(0.5, 0);
          gradient1.EndPoint = new Point(0.5, 1);
  
          gradient1.GradientStops.Add(new GradientStop(Colors.Indigo, 0));
          gradient1.GradientStops.Add(new GradientStop(Colors.Crimson, 0.5));
          gradient1.GradientStops.Add(new GradientStop(Colors.Crimson, 0.5));
          gradient1.GradientStops.Add(new GradientStop(Colors.Indigo, 1));
          if (MultiOrSingle == true)
          {
              foreach (Button elementButton in VisualChildren.FindVisualChildren&lt;Button&gt;(this))
              {
                  if (listBox.Items.Contains(elementButton.Name) == true)
                  {
                      elementButton.Background = gradient1;
                      elementButton.FontWeight = FontWeights.Bold;
                      elementButton.Foreground = Brushes.Gold;
                      elementButton.BorderBrush = Brushes.Gold;
                  }
                  else
                  {
                      elementButton.Background = previousBackgroundColors[elementButton.Name];
                      elementButton.Foreground = previousForegroundColors[elementButton.Name];
                  }
              }
              foreach (Button elementButton in VisualChildren.FindVisualChildren&lt;Button&gt;(this))
              {
                  if (elementButton.Name != "play_quiz" &amp;&amp;
                      elementButton.Name != "show_scoreboard" &amp;&amp;
                      elementButton.Name != "update" &amp;&amp;
                      elementButton.Name != "DragDropGames" &amp;&amp;
                      listBox.Items.Contains(elementButton.Name) == false)
                  {
                      elementButton.Background = Brushes.Gainsboro;
                      elementButton.BorderBrush = Brushes.DarkBlue;
                      elementButton.FontWeight = FontWeights.Normal;
                      elementButton.Foreground = Brushes.Black;
                  }
              }
          }
          else
          {
              foreach (Button elementButton in VisualChildren.FindVisualChildren&lt;Button&gt;(this))
              {
                  if (elementName == elementButton.Name)
                  {
  
                      elementButton.Background = gradient1;
                      elementButton.BorderBrush = Brushes.Gold;
                      elementButton.FontWeight = FontWeights.Bold;
                      elementButton.Foreground = Brushes.Gold;
                  }
                  else
                  {
                      elementButton.Background = previousBackgroundColors[elementButton.Name];
                      elementButton.Foreground = previousForegroundColors[elementButton.Name];
                  }
              }
  
              foreach (Button elementButton in VisualChildren.FindVisualChildren&lt;Button&gt;(this))
              {
                  if (elementButton.Name != "play_quiz" &amp;&amp;
                      elementButton.Name != "show_scoreboard" &amp;&amp;
                      elementButton.Name != "update" &amp;&amp;
                      elementButton.Name != "DragDropGames" &amp;&amp;
                      elementName != elementButton.Name)
                  {
                      elementButton.Background = Brushes.Gainsboro;
                      elementButton.BorderBrush = Brushes.DarkBlue;
                      elementButton.FontWeight = FontWeights.Normal;
                      elementButton.Foreground = Brushes.Black;
                  }
              }
          }
  </code></pre>
  
  <p>with just setting backgrounds brushes etc etc....</p>
  
  <p>However I encountered 2 problems. When doing this in code behind i can change everything but my border brush will remain as it is determined in xaml style I can't change it in code behind don't know why? And after calling this function when I mouse over the buttons they will Shine...however property foreground won't be changed to gold? It will remain black</p>
  
  <p>to demonstrate :</p>
  
  <p><a href="http://i.stack.imgur.com/2EHIr.jpg" rel="nofollow">So this is my mouseover "Shine". When I go with mouse over the button it will change it's look like this</a></p>
  
  <p><a href="http://i.stack.imgur.com/rgzVq.jpg" rel="nofollow">So this is my problem I have searched for H..it came back with few results and called function from above as "multi" and selected all results</a></p>
  
  <p>However this didn't work "elementButton.BorderBrush = Brushes.Gold" it remained transparent as in xaml. And suddenly my Mouseover shine is not the same anymore it's <code>
  &lt;Setter Property="Foreground"  Value="Gold" /&gt;
  &lt;Setter Property="FontWeight"  Value="ExtraBold"/&gt;</code></p>
  
  <p>don't seem to be triggering after the function call</p>
  
  <p>To sum it up. I would like to have the same look of the button and shine effect for all my textbox searches</p>