Occurs when a UIElement receives focus.
Only one element at a time in a page of UI can have focus.
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. For many scenarios 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 key events KeyUp and KeyDown. In some scenarios, apps deliberately 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 Responding to keyboard input.
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 that property displays a focus rectangle. A focus rectangle is an important visual component of a control for accessibility purposes, so that users can see where the current focused element is in the UI and where the user is within the tab sequence of the overall app 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 Input event handlers in controls. For more info on tab sequences and accessibility, see Implementing keyboard accessibility.
Minimum supported client
Minimum supported server
|Windows Server 2012|
- Responding to keyboard input
- Storyboarded animations for visual states
Build date: 11/16/2013