FocusManager 類別

定義

提供一組靜態方法、附加屬性和事件,以判斷並設定焦點範圍,並在範圍內設定可焦點化的項目。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 ref class FocusManager abstract sealed
public static class FocusManager
type FocusManager = class
Public Class FocusManager
繼承
FocusManager

備註

在 Windows Presentation Foundation (WPF)中,有兩個關於焦點的概念:鍵盤焦點和邏輯焦點。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. 具有鍵盤焦點的這個元素 IsKeyboardFocused 設定為 trueThis element with keyboard focus has IsKeyboardFocused set to true. Keyboard.FocusedElement 會傳回具有鍵盤焦點的元素。Keyboard.FocusedElement returns the element with keyboard focus.

邏輯焦點適用于特定焦點範圍內的 FocusManager.FocusedElementLogical focus pertains to the FocusManager.FocusedElement within a specific focus scope.

焦點範圍是一個容器元素,可追蹤其範圍內的 FocusManager.FocusedElementA focus scope is a container element that keeps track of the FocusManager.FocusedElement within its scope. 根據預設,Window 類別是 MenuContextMenuToolBar 類別的焦點範圍。By default, the Window class is a focus scope as are the Menu, ContextMenu, and ToolBar classes. 做為焦點範圍的元素 IsFocusScope 設定為 trueAn 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. 在此情況下,父焦點範圍和子焦點範圍都可以有 FocusManager.FocusedElementIn this case, both the parent focus scope and the child focus scope can have a FocusManager.FocusedElement.

下列案例說明 WPF 應用程式中的鍵盤焦點和邏輯焦點變更,其具有具有 TextBoxWindow,以及具有 MenuItemMenuThe following scenario illustrates how keyboard focus and logical focus change in a WPF application that has a Window with a TextBox and a Menu which has a MenuItem. 當鍵盤焦點從 TextBox 變更為 MenuItem時,TextBox 會遺失鍵盤焦點,但會保留 Window 焦點範圍的邏輯焦點。When keyboard focus changes from the TextBox to the MenuItem, the TextBox losses keyboard focus but retains logical focus for the Window focus scope. MenuItem 取得鍵盤焦點,並取得 Menu 焦點範圍的邏輯焦點。The MenuItem obtains keyboard focus and obtains logical focus for the Menu focus scope. 當鍵盤焦點回到根 Window時,Window 焦點範圍中具有邏輯焦點的元素會取得鍵盤焦點,在此案例中為 TextBoxWhen 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. TextBox 現在具有鍵盤焦點和邏輯焦點。The TextBox now has keyboard focus and logical focus. MenuItem 會失去鍵盤焦點,但會保留 Menu 焦點範圍的邏輯焦點。The MenuItem loses keyboard focus, but retains logical focus for the Menu focus scope.

WindowMenuToolBarContextMenuIsFocusScope 的預設值為 trueThe 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.

欄位

FocusedElementProperty

識別 FocusedElement 附加屬性。Identifies the FocusedElement attached property.

GotFocusEvent

識別 GotFocus 附加事件。Identifies the GotFocus attached event.

IsFocusScopeProperty

識別 IsFocusScope 附加屬性。Identifies the IsFocusScope attached property.

LostFocusEvent

識別 LostFocus 附加事件。Identifies the LostFocus attached event.

附加屬性

FocusedElement

判斷附加這個屬性的項目是否擁有邏輯焦點。Determines whether the element this property is attached to has logical focus.

IsFocusScope

判斷附加這個屬性的項目是否為焦點範圍。Determines whether the element this property is attached to is a focus scope.

方法

AddGotFocusHandler(DependencyObject, RoutedEventHandler)

加入 GotFocus 附加事件的處理常式。Adds a handler for the GotFocus attached event.

AddLostFocusHandler(DependencyObject, RoutedEventHandler)

加入 LostFocus 附加事件的處理常式。Adds a handler for the LostFocus attached event.

GetFocusedElement(DependencyObject)

取得指定焦點範圍內擁有邏輯焦點的項目。Gets the element with logical focus within the specified focus scope.

GetFocusScope(DependencyObject)

判斷指定的項目之最接近的祖系,這個項目會將 IsFocusScope 設定為 trueDetermines the closest ancestor of the specified element that has IsFocusScope set to true.

GetIsFocusScope(DependencyObject)

判斷指定的 DependencyObject 是否為焦點範圍。Determines whether the specified DependencyObject is a focus scope.

RemoveGotFocusHandler(DependencyObject, RoutedEventHandler)

移除 GotFocus 附加事件的處理常式。Removes a handler for the GotFocus attached event.

RemoveLostFocusHandler(DependencyObject, RoutedEventHandler)

移除 LostFocus 附加事件的處理常式。Removes a handler for the LostFocus attached event.

SetFocusedElement(DependencyObject, IInputElement)

在指定項目上設定邏輯焦點。Sets logical focus on the specified element.

SetIsFocusScope(DependencyObject, Boolean)

將指定的 DependencyObject 設定為焦點範圍。Sets the specified DependencyObject as a focus scope.

附加事件

GotFocus

當項目取得焦點時發生。Occurs when an element gets focus.

LostFocus

在項目失去焦點時發生。Occurs when an element loses focus.

適用於