title: WPF TabItem OnMouseOver shall trigger the visibility of a Button inside the TabItem
body: <p>I have a TabControl containing TabItems. I overwrote their ItemTemplate to contain: </p>
  
  <ul>
  <li>A TextBlock showing the caption of the TabItem</li>
  <li>A Button (X) which closes the tab</li>
  </ul>
  
  <p>Now I would like to make the "X"-Button only visible, when the TabItem is either selected or the mouse is over it.</p>
  
  <p>This is my XAML code so far...</p>
  
  <pre><code>&lt;TabControl x:Name="tabControl" 
                      ItemsSource="{Binding OpenedItems}"&gt;
              &lt;TabControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Grid Name="grid" VerticalAlignment="Center"&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition Width="*" /&gt;
                              &lt;ColumnDefinition Width="Auto" /&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
                          &lt;TextBlock Text="{Binding TabCaption}"  
                                     Grid.Column="0"
                                     Foreground="White"
                                     HorizontalAlignment="Left" 
                                     Margin="3" 
                                     Name="label_TabTitle" 
                                     VerticalAlignment="Center" /&gt;
                          &lt;Button Content="X"  
                                  Grid.Column="1"
                                  Foreground="White"
                                  HorizontalAlignment="Right" 
                                  Margin="3" 
                                  Name="button_close" 
                                  VerticalAlignment="Center" 
                                  Width="20" 
                                  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
                                  Visibility="Hidden" 
                                  Click="button_close_Click_1"&gt;
                          &lt;/Button&gt;
                      &lt;/Grid&gt;
  
                      &lt;DataTemplate.Triggers&gt;
                          &lt;Trigger Property="IsMouseOver" Value="True" &gt;
                              &lt;Setter TargetName="button_close" Property="Visibility" Value="Visible"/&gt;
                          &lt;/Trigger&gt;
                      &lt;/DataTemplate.Triggers&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TabControl.ItemTemplate&gt;
              &lt;TabControl.ItemContainerStyle&gt;
                  &lt;Style TargetType="TabItem"&gt;
                      &lt;Setter Property="Background" Value="#1C1C1C" /&gt;
                      &lt;Setter Property="Content" Value="{Binding TabContentView}"/&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type TabItem}"&gt;
                                  &lt;Grid Name="Grid"
                                Background="{StaticResource GrayBrush}"&gt;
                                      &lt;ContentPresenter x:Name="ContentSite"
                                                    VerticalAlignment="Center"
                                                    HorizontalAlignment="Center"
                                                    ContentSource="Header"
                                                    RecognizesAccessKey="True"&gt;
                                      &lt;/ContentPresenter&gt;
                                  &lt;/Grid&gt;
                                  &lt;ControlTemplate.Triggers&gt;
                                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                                          &lt;Setter TargetName="Grid" Property="Background" Value="{StaticResource BlueBrush}" /&gt;
                                      &lt;/Trigger&gt;
                                      &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                                          &lt;Setter TargetName="Grid" Property="Background" Value="{StaticResource LightBlueBrush}" /&gt;
                                      &lt;/Trigger&gt;
                                  &lt;/ControlTemplate.Triggers&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/TabControl.ItemContainerStyle&gt;
          &lt;/TabControl&gt;
  </code></pre>
  
  <p>With the Trigger inside the DataTemplate it kind of works.. but: you have to hover the mouse directly over the Label in order to show the button. So basically, it is impossible to click the button, because it disappears again.</p>
  
  <p>I would appreciate any help with these WPF Templates! </p>
  
  <p>Thanks, Christian</p>