Sdílet prostřednictvím


Control.FocusState Property

Definition

Gets a value that specifies whether this control has focus, and the mode by which focus was obtained.

public:
 property FocusState FocusState { FocusState get(); };
FocusState FocusState();
public FocusState FocusState { get; }
var focusState = control.focusState;
Public ReadOnly Property FocusState As FocusState

Property Value

A value of the enumeration. A value of Unfocused indicates that the control does not have focus.

Remarks

This property supports controls that use different visual styles depending on whether the control was focused by the keyboard or by other means. Some controls use visual states that show a visible focus indicator if the control was focused by a keyboard action, but do not show the indicator if the control was focused by a pointer action. The internal logic of such a control implements OnGotFocus and then gets the value of FocusState from the sender. If FocusState is Keyboard, then a different named visual state that includes a focus rectangle is loaded, by calling GoToState. Typically, focus-related visual states are grouped together in the default control template XAML. Custom controls that want to use different focus visuals depending on the means of focus can implement this same logic themselves. See OnGotFocus.

Another way to use this property is to use FocusManager to determine which element in the overall UI has focus, and then call FocusState on that element to get information on how that control received focus.

Notes for previous versions

Note

In Windows 8, when FocusState is Programmatic, the keyboard focus visual is shown even if the prior method of input was pointer. In Windows 8.1, when you call Focus (FocusState.Programmatic), the prior FocusState, either Pointer or Keyboard, is retained so that the correct focus visual is shown. This means that if you check the value of the FocusState property after you call Focus (FocusState.Programmatic), the FocusState property will have a value of either Pointer or Keyboard.

In an app that’s compiled for Windows 8, but run in Windows 8.1, the Windows 8 behavior is retained. The FocusState property value is Programmatic and the keyboard focus visual is shown.

Note

Classes that inherit from ItemsControl, such as ListBox and AutoSuggestBox, do not propagate the FocusState property. They will always return Unfocused. Access the correct value through RoutedEventArgs.OriginalSource.

Applies to