title: UWP unfocused VisualState overriding selected VisualState on ListViewItem
body: <p>I have a ListView that is using a custom style on its ItemContainerStyle property.</p>
  <p>The problem is that when I use a keyboard to navigate through the items in the list, if I navigate to the currently-selected item, it gets the focused style (which is good), but then the moment I navigate away from that item it starts using the unfocused style instead of the selected style.</p>
  <p>For example, let's say item 3 out of 5 is selected in the ListView.  It'll look like this before the list gets focus, which is correct:</p>
  <p><a href="https://i.stack.imgur.com/tErTD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tErTD.png" alt="third item getting properly styled with selected unfocused state" /></a></p>
  <p>Then I use my keyboard to navigate to the list, which applies the &quot;focused&quot; style to the selected item, which is fine...</p>
  <p><a href="https://i.stack.imgur.com/30qL3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/30qL3.png" alt="third item getting properly styled with focused state" /></a></p>
  <p>But then I navigate down by pressing the down arrow key, and suddenly the third item is no longer looks &quot;selected.&quot;  It looks like it's just a normal unselected item:</p>
  <p><a href="https://i.stack.imgur.com/6a8Zz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6a8Zz.png" alt="third item incorrectly getting unfocused style applied when I want selected style" /></a></p>
  <p>This is happening because the &quot;unfocused&quot; state is overriding the &quot;selected&quot; visual state of the third item, but I want it to be the other way around.</p>
  <p>Note: I have SingleSelectionFollowsFocus=&quot;False&quot; on the ListView.</p>
  <p>If I remove the &quot;unselected&quot; state, then moving the focus from the third item makes any item that I navigate to look like it's currently getting the focused state... if I could figure out why this is happening, then I could safely remove the &quot;unselected&quot; state, and it would work fine.</p>
  <p><a href="https://i.stack.imgur.com/LtdDS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LtdDS.png" alt="multiple items getting focus state for some reason" /></a></p>
  <p>How can I retain the &quot;selected&quot; visual state of the selected item after unfocusing it?</p>
  <p>Here's my code:</p>
  <p>XAML:</p>
  <pre><code>&lt;ListView x:Name=&quot;ThingieListView&quot;
                                    ItemsSource=&quot;{x:Bind viewModel.ThingieDataList}&quot;
                                    SingleSelectionFollowsFocus=&quot;False&quot;
                                    SelectionMode=&quot;Single&quot;
                                    IsItemClickEnabled=&quot;True&quot;
                                    XYFocusRight=&quot;{x:Bind LocationListView}&quot;
                                    Margin=&quot;8&quot;
                                    Width=&quot;256&quot;
                                    IsTabStop=&quot;True&quot;
                                    TabIndex=&quot;0&quot;
                                    LostFocus=&quot;ThingieListView_LostFocus&quot;
                                    GotFocus=&quot;ThingieListView_GotFocus&quot;
                                    ItemClick=&quot;ThingieListView_OnItemClick&quot;
                                    ItemContainerStyle=&quot;{StaticResource SelectThingieItemStyle}&quot;&gt;
                                  &lt;ListView.ItemTemplate&gt;
                                      &lt;DataTemplate&gt;
                                          &lt;ListViewItem Content=&quot;{Binding DisplayValue}&quot;  /&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/ListView.ItemTemplate&gt;
                              &lt;/ListView&gt; 
  </code></pre>
  <p>Custom Style:</p>
  <pre><code>&lt;Style x:Key=&quot;SelectThingieItemStyle&quot; TargetType=&quot;ListViewItem&quot;&gt;
          &lt;Setter Property=&quot;FontFamily&quot; Value=&quot;{StaticResource Res-FontFamily}&quot;/&gt;
          &lt;Setter Property=&quot;FontSize&quot; Value=&quot;{StaticResource FontSizeText2}&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;{ThemeResource ListViewItemBackground}&quot;/&gt;
          &lt;Setter Property=&quot;Foreground&quot; Value=&quot;{ThemeResource ListViewItemForeground}&quot;/&gt;
          &lt;Setter Property=&quot;TabNavigation&quot; Value=&quot;Local&quot;/&gt;
          &lt;Setter Property=&quot;IsHoldingEnabled&quot; Value=&quot;True&quot;/&gt;
          &lt;Setter Property=&quot;Padding&quot; Value=&quot;0,0,0,0&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot;/&gt;
          &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;MinWidth&quot; Value=&quot;{ThemeResource ListViewItemMinWidth}&quot;/&gt;
          &lt;Setter Property=&quot;MinHeight&quot; Value=&quot;{ThemeResource ListViewItemMinHeight}&quot;/&gt;
          &lt;Setter Property=&quot;AllowDrop&quot; Value=&quot;False&quot;/&gt;
          &lt;Setter Property=&quot;UseSystemFocusVisuals&quot; Value=&quot;{StaticResource UseSystemFocusVisuals}&quot;/&gt;
          &lt;Setter Property=&quot;FocusVisualMargin&quot; Value=&quot;0&quot;/&gt;
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;ListViewItem&quot;&gt;
                      &lt;ListViewItemPresenter x:Name=&quot;Root&quot;
                                             CheckBrush=&quot;{ThemeResource ListViewItemCheckBrush}&quot;
                                             ContentMargin=&quot;15&quot;
                                             Height=&quot;63&quot;
                                             Margin=&quot;0 0 0 1&quot;
                                             CheckBoxBrush=&quot;{ThemeResource ListViewItemCheckBoxBrush}&quot;
                                             ContentTransitions=&quot;{TemplateBinding ContentTransitions}&quot;
                                             CheckMode=&quot;{ThemeResource ListViewItemCheckMode}&quot;
                                             DragOpacity=&quot;{ThemeResource ListViewItemDragThemeOpacity}&quot;
                                             DisabledOpacity=&quot;{ThemeResource ListViewItemDisabledThemeOpacity}&quot;
                                             DragBackground=&quot;{ThemeResource ListViewItemDragBackground}&quot;
                                             DragForeground=&quot;{ThemeResource ListViewItemDragForeground}&quot;
                                             FocusBorderBrush=&quot;{ThemeResource ListViewItemFocusBorderBrush}&quot;
                                             FocusVisualMargin=&quot;{TemplateBinding FocusVisualMargin}&quot;
                                             FocusVisualPrimaryThickness=&quot;0&quot;
                                             FocusSecondaryBorderBrush=&quot;{ThemeResource ListViewItemFocusSecondaryBorderBrush}&quot;
                                             HorizontalContentAlignment=&quot;{TemplateBinding HorizontalContentAlignment}&quot;
                                             Control.IsTemplateFocusTarget=&quot;True&quot;
                                             PressedBackground=&quot;{ThemeResource ListViewItemBackgroundPressed}&quot;
                                             PlaceholderBackground=&quot;{ThemeResource ListViewItemPlaceholderBackground}&quot;
                                             PointerOverForeground=&quot;{ThemeResource ListViewItemForegroundPointerOver}&quot;
                                             PointerOverBackground=&quot;{ThemeResource ListViewItemBackgroundPointerOver}&quot;
                                             RevealBorderThickness=&quot;{ThemeResource ListViewItemRevealBorderThemeThickness}&quot;
                                             ReorderHintOffset=&quot;{ThemeResource ListViewItemReorderHintThemeOffset}&quot;
                                             RevealBorderBrush=&quot;{ThemeResource ListViewItemRevealBorderBrush}&quot;
                                             RevealBackground=&quot;{ThemeResource ListViewItemRevealBackground}&quot;
                                             SelectedForeground=&quot;{ThemeResource ListViewItemForegroundSelected}&quot;
                                             SelectionCheckMarkVisualEnabled=&quot;{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}&quot;
                                             SelectedBackground=&quot;{ThemeResource ListViewItemBackgroundSelected}&quot;
                                             SelectedPressedBackground=&quot;{ThemeResource ListViewItemBackgroundSelectedPressed}&quot;
                                             SelectedPointerOverBackground=&quot;{ThemeResource ListViewItemBackgroundSelectedPointerOver}&quot;
                                             VerticalContentAlignment=&quot;{TemplateBinding VerticalContentAlignment}&quot;&gt;
                          &lt;VisualStateManager.VisualStateGroups&gt;
                              &lt;VisualStateGroup x:Name=&quot;CommonStates&quot;&gt;
                                  &lt;VisualState x:Name=&quot;Normal&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_2}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Gray_1}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;0&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;Selected&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Black}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.FontWeight&quot; Value=&quot;Bold&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;0&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_4}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;PointerOver&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_2}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;3&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;PointerOverSelected&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_4}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;3&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;PointerOverPressed&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_2}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;3&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-Black}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;Pressed&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;3&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.FontWeight&quot; Value=&quot;Bold&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;PressedSelected&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;3&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.FontWeight&quot; Value=&quot;Bold&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                              &lt;/VisualStateGroup&gt;
                              &lt;VisualStateGroup x:Name=&quot;FocusedStates&quot;&gt;
                                  &lt;VisualState x:Name=&quot;Focused&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.FontWeight&quot; Value=&quot;Bold&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-Black}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;0 0 20 0&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Orange}&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                                  &lt;VisualState x:Name=&quot;Unfocused&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.Foreground&quot; Value=&quot;{StaticResource Res-White}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBackground&quot; Value=&quot;{StaticResource Res-Gray_2}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderBrush&quot; Value=&quot;{StaticResource Res-Gray_1}&quot;/&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;0 1 0 1&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                              &lt;/VisualStateGroup&gt;
                              &lt;VisualStateGroup x:Name=&quot;DisabledStates&quot;&gt;
                                  &lt;VisualState x:Name=&quot;Enabled&quot;/&gt;
                                  &lt;VisualState x:Name=&quot;Disabled&quot;&gt;
                                      &lt;VisualState.Setters&gt;
                                          &lt;Setter Target=&quot;Root.RevealBorderThickness&quot; Value=&quot;0&quot;/&gt;
                                      &lt;/VisualState.Setters&gt;
                                  &lt;/VisualState&gt;
                              &lt;/VisualStateGroup&gt;
                          &lt;/VisualStateManager.VisualStateGroups&gt;
                      &lt;/ListViewItemPresenter&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          
      &lt;/Style&gt; 
  </code></pre>