Share via


FocusManager Class

Definition

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

public ref class FocusManager sealed
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class FocusManager final
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class FocusManager
Public NotInheritable Class FocusManager
Inheritance
Object Platform::Object IInspectable FocusManager
Attributes

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 instead of FocusManager events whenever 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)

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

FindLastFocusableElement(DependencyObject)

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

FindNextElement(FocusNavigationDirection, FindNextElementOptions)

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

FindNextElement(FocusNavigationDirection)

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

FindNextFocusableElement(FocusNavigationDirection, Rect)

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

FindNextFocusableElement(FocusNavigationDirection)

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

GetFocusedElement()

Retrieves the element in the UI that has focus.

GetFocusedElement(XamlRoot)

Retrieves the focused element within the XAML island container.

TryFocusAsync(DependencyObject, FocusState)

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

TryMoveFocus(FocusNavigationDirection, FindNextElementOptions)

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

TryMoveFocus(FocusNavigationDirection)

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

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.

TryMoveFocusAsync(FocusNavigationDirection)

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

Events

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

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

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

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.

Applies to

See also