@@ -1606,39 +1606,43 @@ private protected void SetPointerOver(bool value, bool callChangeVisualState = t
16061606 /// </summary>
16071607 protected internal virtual void ChangeVisualState ( )
16081608 {
1609- // States regarding a control's enabled or pointer events influence the main style
1610- // of the element.
1611- //
1612- // Disabled, PointerOver, Pressed and Normal are typically put into the
1613- // CommonStates visual state group.
1614- if ( ! IsEnabled )
1609+ // A disabled control should never be in a focused state as part of the feature
1610+ // of being disabled is that it cannot receive focus. If it was in focus, then
1611+ // it has to go out of focus.
1612+ var shouldFocus = IsFocused && IsEnabled ;
1613+
1614+ // If the control cannot have focus, make sure it appears unfocused by moving to
1615+ // the Unfocused state.
1616+ if ( ! shouldFocus )
16151617 {
1616- VisualStateManager . GoToState ( this , VisualStateManager . CommonStates . Disabled ) ;
1618+ VisualStateManager . GoToState ( this , VisualStateManager . FocusStates . Unfocused ) ;
16171619 }
1618- else if ( IsPointerOver )
1620+
1621+ // Set the Disabled or Normal states depending on the value of IsEnabled and
1622+ // IsPointerOver. We set the PointerOver state later, after the Focused state.
1623+ if ( ! IsEnabled )
16191624 {
1620- VisualStateManager . GoToState ( this , VisualStateManager . CommonStates . PointerOver ) ;
1625+ VisualStateManager . GoToState ( this , VisualStateManager . CommonStates . Disabled ) ;
16211626 }
1622- else
1627+ else if ( ! IsPointerOver )
16231628 {
16241629 VisualStateManager . GoToState ( this , VisualStateManager . CommonStates . Normal ) ;
16251630 }
16261631
1627- // Focus needs to be handled independently; otherwise, if no actual Focus state is supplied
1628- // in the control's visual states, the state can end up stuck in PointerOver after the pointer
1629- // exits and the control still has focus.
1630- //
1631- // Focused and Unfocused states are typically put into the FocusStates visual state group.
1632- if ( IsFocused && IsEnabled )
1632+ // Go to the Focus state after the Normal state, so that the Focus state can
1633+ // override the Normal state's properties if a control is both focused and
1634+ // hovered.
1635+ if ( shouldFocus )
16331636 {
16341637 VisualStateManager . GoToState ( this , VisualStateManager . FocusStates . Focused ) ;
16351638 }
1636- else
1639+
1640+ // The PointerOver state is applied last so that it can override all the states. Even
1641+ // though this state is separate here, it should still be part of the CommonStates
1642+ // visual state group.
1643+ if ( IsPointerOver )
16371644 {
1638- // A disabled control should never be in a focused state as part of the feature
1639- // of being disabled is that it cannot receive focus. If it was in focus, then
1640- // it has to go out of focus.
1641- VisualStateManager . GoToState ( this , VisualStateManager . FocusStates . Unfocused ) ;
1645+ VisualStateManager . GoToState ( this , VisualStateManager . CommonStates . PointerOver ) ;
16421646 }
16431647 }
16441648
0 commit comments