Delen via


UIElement.GotFocus Event

Definition

Occurs when a UIElement receives focus. This event is raised asynchronously, so focus can move again before bubbling is complete.

// Register
event_token GotFocus(RoutedEventHandler const& handler) const;

// Revoke with event_token
void GotFocus(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::GotFocus_revoker GotFocus(auto_revoke_t, RoutedEventHandler const& handler) const;
public event RoutedEventHandler GotFocus;
function onGotFocus(eventArgs) { /* Your code */ }
uIElement.addEventListener("gotfocus", onGotFocus);
uIElement.removeEventListener("gotfocus", onGotFocus);
- or -
uIElement.ongotfocus = onGotFocus;
Public Custom Event GotFocus As RoutedEventHandler 
<uiElement GotFocus="eventhandler"/>

Event Type

Remarks

We recommend using the UIElement focus routed events instead of FocusManager events whenever possible.

Only a single UI element at a time can have focus.

A control can get focus when another control loses focus, the application view changes, the user switches applications, or the user interacts with the system such that the application is no longer in the foreground.

LostFocus is raised before GotFocus.

If you are using control compositing or UI compositing and handling GotFocus on a container such as a Panel or GridView, then you might want to check the OriginalSource on the event data to determine which element in the composition actually received the focus.

GotFocus doesn't have any specialized event data. Where you handle GotFocus, you might want to determine whether it was the user or app code that caused an element to gain focus. To determine this in your handler, cast the sender of the event to be a Control object, and check the FocusState.

Keyboard focus is particularly relevant for keyboard event handling, because only the currently keyboard-focused UIElement can be the source of the KeyUp and KeyDown key events. Applications might call Focus as an initialization action so that the key events in the app as a whole can be used to detect access or accelerator keys. For more info, see Keyboard interactions.

GotFocus is a routed event. For more info on the routed event concept, see Events and routed events overview.

Specific Windows Runtime controls may have class-based handling for the GotFocus event. If so, the control probably has an override for the method OnGotFocus. Typically the event is marked handled by the class handler, and the GotFocus event is not raised for handling by any user code handlers on that control. Controls might handle the event in order to load a visual state property that displays a focus rectangle.

A control's focus rectangle is an important visual component that aids accessibility by identifying the current focused element and indicating where the user is within the tab sequence of the application UI. In some cases, the FocusState of the focus action is relevant for whether a visible focus indicator should display.

For more info on how class-based handling for events works, see Events and routed events overview. For more info on tab sequences and accessibility, see Keyboard accessibility.

Applies to

See also