FocusManager Class

Definition

Provides a set of static methods, attached properties, and events for determining and setting focus scopes and for setting the focused element within the scope.

public static class FocusManager
Inheritance
FocusManager

Remarks

In Windows Presentation Foundation (WPF) there are two concepts concerning focus: keyboard focus and logical focus.

Keyboard focus pertains to the element which is currently receiving keyboard input. There can be only one element with keyboard focus. This element with keyboard focus has IsKeyboardFocused set to true. Keyboard.FocusedElement returns the element with keyboard focus.

Logical focus pertains to the FocusedElement within a specific focus scope.

A focus scope is a container element that keeps track of the FocusedElement within its scope. By default, the Window class is a focus scope as are the Menu, ContextMenu, and ToolBar classes. An element which is a focus scope has IsFocusScope set to true.

There can be multiple elements with logical focus, but there can only be one element with logical focus within a single focus scope. An element with logical focus does not necessarily have keyboard focus, but an element with keyboard focus will have logical focus. It is possible to define a focus scope within a focus scope. In this case, both the parent focus scope and the child focus scope can have a FocusedElement.

The following scenario illustrates how keyboard focus and logical focus change in a Windows Presentation Foundation (WPF) application that has a Window with a TextBox and a Menu which has a MenuItem. When keyboard focus changes from the TextBox to the MenuItem, the TextBox losses keyboard focus but retains logical focus for the Window focus scope. The MenuItem obtains keyboard focus and obtains logical focus for the Menu focus scope. When keyboard focus returns to the root Window, the element in Window focus scope with logical focus will obtain keyboard focus, which in this case is the TextBox. The TextBox now has keyboard focus and logical focus. The MenuItem loses keyboard focus, but retains logical focus for the Menu focus scope.

The default value of IsFocusScope on a Window, Menu, ToolBar, and ContextMenu is true.

For more information on focus, see the Input Overview and the Focus Overview.

Fields

FocusedElementProperty

Identifies the System.Windows.Input.FocusManager.FocusedElement attached property.

GotFocusEvent

Identifies the System.Windows.Input.FocusManager.GotFocus attached event.

IsFocusScopeProperty

Identifies the System.Windows.Input.FocusManager.IsFocusScope attached property.

LostFocusEvent

Identifies the System.Windows.Input.FocusManager.LostFocus attached event.

Methods

AddGotFocusHandler(DependencyObject, RoutedEventHandler)

Adds a handler for the System.Windows.Input.FocusManager.GotFocus attached event.

AddLostFocusHandler(DependencyObject, RoutedEventHandler)

Adds a handler for the System.Windows.Input.FocusManager.LostFocus attached event.

Equals(Object) Inherited from Object
Equals(Object, Object) Inherited from Object
GetFocusScope(DependencyObject)

Determines the closest ancestor of the specified element that has System.Windows.Input.FocusManager.IsFocusScope set to true.

GetFocusedElement(DependencyObject)

Gets the element with logical focus within the specified focus scope.

GetHashCode() Inherited from Object
GetIsFocusScope(DependencyObject)

Determines whether the specified DependencyObject is a focus scope.

GetType() Inherited from Object
MemberwiseClone() Inherited from Object
ReferenceEquals(Object, Object) Inherited from Object
RemoveGotFocusHandler(DependencyObject, RoutedEventHandler)

Removes a handler for the System.Windows.Input.FocusManager.GotFocus attached event.

RemoveLostFocusHandler(DependencyObject, RoutedEventHandler)

Removes a handler for the System.Windows.Input.FocusManager.LostFocus attached event.

SetFocusedElement(DependencyObject, IInputElement)

Sets logical focus on the specified element.

SetIsFocusScope(DependencyObject, Boolean)

Sets the specified DependencyObject as a focus scope.

ToString() Inherited from Object

Applies to