title: WPF, TabControl, selection and hover states on TabItem
body: <p>I have a TabControl in an MVVM WPF application. I created template for TabItem and TabControl
  based on this msdn <a href="http://msdn.microsoft.com/en-us/library/ms754137%28v=vs.110%29.aspx" rel="nofollow">topic</a>. I made some modification and I added few more VisualStatManagers states for supporting selection and hover states on tabItems.</p>
  
  <p><strong>Here's my ItemControl template</strong></p>
  
  <pre><code>    &lt;Style x:Key="TabItemStyle" TargetType="TabItem"&gt;
      &lt;Setter Property="Background" Value="{StaticResource TabControlBackgroundBrush}" /&gt;
      &lt;Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" /&gt;
      &lt;Setter Property="FontSize" Value="13.333" /&gt;
      &lt;Setter Property="FontFamily" Value="Segoe UI" /&gt;
      &lt;Setter Property="MinHeight" Value="30" /&gt;
      &lt;Setter Property="Padding" Value="6,2" /&gt;
      &lt;Setter Property="Margin" Value="0" /&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Stretch" /&gt;
      &lt;Setter Property="BorderThickness" Value="1" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="TabItem"&gt;
                  &lt;Grid&gt;
                      &lt;VisualStateManager.VisualStateGroups&gt;
                          &lt;VisualStateGroup x:Name="CommonStates"&gt;
                              &lt;VisualState x:Name="Normal" /&gt;
                              &lt;VisualState x:Name="MouseOver"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="Bd" Storyboard.TargetProperty="Fill"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TabItemHoverBackgroundBrush}" /&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="Bd" Storyboard.TargetProperty="Stroke"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TabItemHoverBorderBrush}" /&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentControl"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource LightForegroundBrush}" /&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="Disabled"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="DisabledVisualElement"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                          &lt;/VisualStateGroup&gt;
                          &lt;VisualStateGroup x:Name="SelectionStates"&gt;
                              &lt;VisualState x:Name="Unselected" /&gt;
                              &lt;VisualState x:Name="Selected"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="BgSelected"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentControl"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Collapsed&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentControl1"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentControl1"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource LightForegroundBrush}" /&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="SelectedUnfocused"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="BgSelected"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentControl"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource LightForegroundBrush}" /&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                          &lt;/VisualStateGroup&gt;
                          &lt;VisualStateGroup x:Name="FocusStates"&gt;
                              &lt;VisualState x:Name="Focused"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FocusedVisualElement"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="Unfocused" /&gt;
                          &lt;/VisualStateGroup&gt;
                      &lt;/VisualStateManager.VisualStateGroups&gt;
                      &lt;Rectangle x:Name="Bd" Fill="{TemplateBinding Background}" Stroke="{StaticResource TabItemHoverBorderBrush}" StrokeThickness="1" /&gt;
                      &lt;Rectangle x:Name="BgSelected" Fill="{StaticResource TabItemSelectedBackgroundBrush}" Stroke="{StaticResource TabItemSelectedBorderBrush}" Visibility="Collapsed" /&gt;
                      &lt;ContentControl x:Name="contentControl" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" Foreground="{TemplateBinding Foreground}"&gt;
                          &lt;ContentPresenter x:Name="ContentSite"
                            ContentSource="Header"
                            RecognizesAccessKey="True"
                            Margin="-1,-1,-1,0"
                            VerticalAlignment="Center"/&gt;
                      &lt;/ContentControl&gt;
                      &lt;ContentControl x:Name="contentControl1" Visibility="Collapsed" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" Foreground="{StaticResource ForegroundBrush}"&gt;
                          &lt;ContentPresenter x:Name="ContentSite1"
                            ContentSource="Header"
                            RecognizesAccessKey="True"
                            Margin="-5,-5,-5,0"
                            VerticalAlignment="Center"/&gt;
                      &lt;/ContentControl&gt;
                      &lt;Rectangle x:Name="FocusedVisualElement" IsHitTestVisible="False" Visibility="Collapsed" Stroke="{StaticResource TabControlPressedBorderBrush}" StrokeThickness="2"/&gt;
                      &lt;Rectangle x:Name="DisabledVisualElement" Fill="{StaticResource DisabledVisualElement}" Visibility="Collapsed"/&gt;
                  &lt;/Grid&gt;
                  &lt;!--&lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                          &lt;Setter Property="Panel.ZIndex" Value="100" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;--&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p><strong>And my TabControl template</strong></p>
  
  <pre><code>&lt;Style x:Key="TabControlStyle" TargetType="TabControl"&gt;
      &lt;Setter Property="Padding" Value="0" /&gt;
      &lt;Setter Property="Background" Value="{StaticResource TabControlBackgroundBrush}" /&gt;
      &lt;Setter Property="BorderBrush" Value="{StaticResource TabControlBorderBrush}" /&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Left" /&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Top" /&gt;
      &lt;Setter Property="IsTabStop" Value="False" /&gt;
      &lt;Setter Property="BorderThickness" Value="1" /&gt;
      &lt;!--&lt;Setter Property="ItemContainerStyle" Value="{StaticResource TabItemStyle}" /&gt;--&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="TabControl"&gt;
                  &lt;Grid KeyboardNavigation.TabNavigation="Local"&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="Auto" /&gt;
                          &lt;RowDefinition Height="*" /&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;VisualStateManager.VisualStateGroups&gt;
                          &lt;VisualStateGroup x:Name="ValidationStates"&gt;
                              &lt;VisualState x:Name="Valid" /&gt;
                              &lt;VisualState x:Name="InvalidUnfocused"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="InvalidFocused"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip"&gt;
                                          &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                              &lt;DiscreteObjectKeyFrame.Value&gt;
                                                  &lt;System:Boolean&gt;True&lt;/System:Boolean&gt;
                                              &lt;/DiscreteObjectKeyFrame.Value&gt;
                                          &lt;/DiscreteObjectKeyFrame&gt;
                                      &lt;/ObjectAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                          &lt;/VisualStateGroup&gt;
                      &lt;/VisualStateManager.VisualStateGroups&gt;
                      &lt;!--Tabs strip--&gt;
                      &lt;TabPanel x:Name="HeaderPanel" Grid.Row="0" Panel.ZIndex="1" IsItemsHost="True" KeyboardNavigation.TabIndex="1" Background="Transparent" Margin="0,0,4,-1"/&gt;
                      &lt;!--Border of the content--&gt;
                      &lt;Border x:Name="Border" Grid.Row="1" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.DirectionalNavigation="Contained" KeyboardNavigation.TabIndex="2" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"&gt;
                          &lt;ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ScrollViewerStyle}"&gt;
                              &lt;ContentPresenter Name="PART_SelectedContentHost" Margin="4" ContentSource="SelectedContent" /&gt;
                          &lt;/ScrollViewer&gt;
                      &lt;/Border&gt;
                      &lt;Border x:Name="ValidationErrorElement" Grid.Row="1" BorderBrush="{StaticResource ValidationErrorElement}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Visibility="Collapsed"&gt;
                          &lt;ToolTipService.ToolTip&gt;
                              &lt;ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}"&gt;
                                  &lt;ToolTip.Triggers&gt;
                                      &lt;EventTrigger RoutedEvent="Canvas.Loaded"&gt;
                                          &lt;BeginStoryboard&gt;
                                              &lt;Storyboard&gt;
                                                  &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip"&gt;
                                                      &lt;DiscreteObjectKeyFrame KeyTime="0"&gt;
                                                          &lt;DiscreteObjectKeyFrame.Value&gt;
                                                              &lt;System:Boolean&gt;true&lt;/System:Boolean&gt;
                                                          &lt;/DiscreteObjectKeyFrame.Value&gt;
                                                      &lt;/DiscreteObjectKeyFrame&gt;
                                                  &lt;/ObjectAnimationUsingKeyFrames&gt;
                                              &lt;/Storyboard&gt;
                                          &lt;/BeginStoryboard&gt;
                                      &lt;/EventTrigger&gt;
                                  &lt;/ToolTip.Triggers&gt;
                              &lt;/ToolTip&gt;
                          &lt;/ToolTipService.ToolTip&gt;
                          &lt;Grid Background="{StaticResource TransparentBrush}" HorizontalAlignment="Right" Height="10" Margin="0,-4,-4,0" VerticalAlignment="Top" Width="10"&gt;
                              &lt;Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="{StaticResource ValidationErrorElement}" Margin="-1,3,0,0" /&gt;
                              &lt;Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="{StaticResource LightForegroundBrush}" Margin="-1,3,0,0" /&gt;
                          &lt;/Grid&gt;
                      &lt;/Border&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>My problem is strange behavior of my template in my project.
  In VS2012 in xaml designer TabControl looks normal. But it's completely opposite in compiled application.
  The selection state don't work correctly. In fact it presents only normal vsm state. 
  I can switch between TabItems content but Selection and Hover (mouseOver) state on tab strip are not changing background and border colors. As I said only normal state of VisualStateManager is working in executed app.
  Since we cant debug wpf vsm I cant figured out what causes that problem.
  I think it is a problem with VisualSateManager.
  For test i used single windows with simple tab control posted below</p>
  
  <pre><code>&lt;TabControl&gt;
     &lt;TabItem Header="A"&gt;content A&lt;/TabItem&gt;
     &lt;TabItem Header="B"&gt;content B&lt;/TabItem&gt;
     &lt;TabItem Header="C"&gt;content C&lt;/TabItem&gt;
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>If you know what causes a problem with my app, or you have met with similar situation with controls in wpf pleas help me.</p>