FocusManager FocusManager FocusManager FocusManager Class

Definition

A helper class that enables global management of focus actions and events across all elements in an application.

public : sealed class FocusManager : IFocusManager
struct winrt::Windows::UI::Xaml::Input::FocusManager : IFocusManager
public sealed class FocusManager : IFocusManager
Public NotInheritable Class FocusManager Implements IFocusManager
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

Use TryMoveFocus to traverse between UI elements using the arrow keys.

private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == Windows.System.VirtualKey.Up)
   {
      // Mimic Shift+Tab when user hits up arrow key.
      FocusManager.TryMoveFocus(FocusNavigationDirection.Previous);
   }
   else if (e.Key == Windows.System.VirtualKey.Down)
   {
      // Mimic Tab when user hits down arrow key.
      FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
   }
}

Remarks

We recommend using the UIElement focus routed events when possible.

FocusManager is intended for advanced scenarios where specific elements in an application do not receive bubbled events from a UIElement. For example, visual "overlay" objects such as Popup, Tooltip, or MenuFlyout that appear as part of the app UI but are actually their own focus scope and not part of the visual tree hierarchy. In the case of a Popup, this occurs when you programmatically set IsOpen to true and focus moves in and out of the popup.

Here's some basic markup to illustrate the example:

<Page …>
    <Grid …>
        <Popup …>
            <StackPanel Name="StackPanel3" …>
                <TextBlock Text="StackPanel3" />
                <Button Content="Button5" …/>
            </StackPanel>
        </Popup>
        <TextBlock Text="Grid" … />
        <StackPanel Name="StackPanel1" …>
            <TextBlock Text="StackPanel1" …/>
            <Button Content="Button1" …/>
            <Button Content="Button2" … />
        </StackPanel>
        <StackPanel Name="StackPanel2" …>
            <TextBlock Text="StackPanel2" …/>
            <Button Content="Button3" … />
            <Button Content="Button4" … />
        </StackPanel>
    </Grid>
</Page>

In this case, a Popup contains StackPanel3, which, in turn, contains Button5. When IsOpen is set to true and focus moves from Button1 to Button5, the LosingFocus and LostFocus events bubble up across the visual tree (StackPanel1, the Grid, and the Page get both these events). However, only StackPanel3 receives the subsequent GettingFocus and GotFocus events that bubble up from Button5 (Grid and Page do not, as they are not in a parent-child relationship with Popup).

An application can have multiple elements with logical focus (depending on the number of focus scopes). However, only one element in the application can have keyboard focus.

There can be multiple elements with logical focus, but only one element with logical focus per focus scope. An element with logical focus does not necessarily have keyboard focus, but an element with keyboard focus does have logical focus.

Methods

FindFirstFocusableElement(DependencyObject) FindFirstFocusableElement(DependencyObject) FindFirstFocusableElement(DependencyObject) FindFirstFocusableElement(DependencyObject)

Retrieves the first element that can receive focus based on the specified scope.

FindLastFocusableElement(DependencyObject) FindLastFocusableElement(DependencyObject) FindLastFocusableElement(DependencyObject) FindLastFocusableElement(DependencyObject)

Retrieves the last element that can receive focus based on the specified scope.

FindNextElement(FocusNavigationDirection) FindNextElement(FocusNavigationDirection) FindNextElement(FocusNavigationDirection) FindNextElement(FocusNavigationDirection)

Retrieves the element that should receive focus based on the specified navigation direction.

FindNextElement(FocusNavigationDirection, FindNextElementOptions) FindNextElement(FocusNavigationDirection, FindNextElementOptions) FindNextElement(FocusNavigationDirection, FindNextElementOptions) FindNextElement(FocusNavigationDirection, FindNextElementOptions)

Retrieves the element that should receive focus based on the specified navigation direction (cannot be used with tab navigation, see remarks).

FindNextFocusableElement(FocusNavigationDirection) FindNextFocusableElement(FocusNavigationDirection) FindNextFocusableElement(FocusNavigationDirection) FindNextFocusableElement(FocusNavigationDirection)

Retrieves the element that should receive focus based on the specified navigation direction.

FindNextFocusableElement(FocusNavigationDirection, Rect) FindNextFocusableElement(FocusNavigationDirection, Rect) FindNextFocusableElement(FocusNavigationDirection, Rect) FindNextFocusableElement(FocusNavigationDirection, Rect)

Retrieves the element that should receive focus based on the specified navigation direction and hint rectangle.

GetFocusedElement() GetFocusedElement() GetFocusedElement() GetFocusedElement()

Retrieves the element in the UI that has focus.

TryFocusAsync(DependencyObject, FocusState) TryFocusAsync(DependencyObject, FocusState) TryFocusAsync(DependencyObject, FocusState) TryFocusAsync(DependencyObject, FocusState)

Asynchronously attempts to set focus on an element when the application is initialized.

TryMoveFocus(FocusNavigationDirection) TryMoveFocus(FocusNavigationDirection) TryMoveFocus(FocusNavigationDirection) TryMoveFocus(FocusNavigationDirection)

Attempts to change focus from the element with focus to the next focusable element in the specified direction.

TryMoveFocus(FocusNavigationDirection, FindNextElementOptions) TryMoveFocus(FocusNavigationDirection, FindNextElementOptions) TryMoveFocus(FocusNavigationDirection, FindNextElementOptions) TryMoveFocus(FocusNavigationDirection, FindNextElementOptions)

Attempts to change focus from the element with focus to the next focusable element in both the specified direction and using the specified navigation options.

TryMoveFocusAsync(FocusNavigationDirection) TryMoveFocusAsync(FocusNavigationDirection) TryMoveFocusAsync(FocusNavigationDirection) TryMoveFocusAsync(FocusNavigationDirection)

Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction.

TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions) TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions) TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions) TryMoveFocusAsync(FocusNavigationDirection, FindNextElementOptions)

Asynchronously attempts to change focus from the current element with focus to the next focusable element in the specified direction and subject to the specified navigation options.

Events

GettingFocus GettingFocus GettingFocus GettingFocus

Occurs before an element actually receives focus. This event is raised synchronously to ensure focus isn't moved while the event is bubbling.

GotFocus GotFocus GotFocus GotFocus

Occurs when an element within a container element (a focus scope) receives focus. This event is raised asynchronously, so focus might move before bubbling is complete.

LosingFocus LosingFocus LosingFocus LosingFocus

Occurs before focus moves from the current element with focus to the target element. This event is raised synchronously to ensure focus isn't moved while the event is bubbling.

LostFocus LostFocus LostFocus LostFocus

Occurs when an element within a container element (a focus scope) loses focus. This event is raised asynchronously, so focus might move again before bubbling is complete.