Dela via


UIElement.LostFocus Event

Definition

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

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

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

// Revoke with event_revoker
UIElement::LostFocus_revoker LostFocus(auto_revoke_t, RoutedEventHandler const& handler) const;
public event RoutedEventHandler LostFocus;
function onLostFocus(eventArgs) { /* Your code */ }
uIElement.addEventListener("lostfocus", onLostFocus);
uIElement.removeEventListener("lostfocus", onLostFocus);
- or -
uIElement.onlostfocus = onLostFocus;
Public Custom Event LostFocus As RoutedEventHandler 
<uiElement LostFocus="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 lose focus when another control gets 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.

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.

LostFocus is a routed event. For more info on how class-based handling for events works, see Events and routed events overview.

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

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 tab sequences and accessibility, see Keyboard accessibility.

Applies to

See also