FocusManager.SetFocusedElement(DependencyObject, IInputElement) 方法

定义

对指定元素设置逻辑焦点。Sets logical focus on the specified element.

public:
 static void SetFocusedElement(System::Windows::DependencyObject ^ element, System::Windows::IInputElement ^ value);
public static void SetFocusedElement (System.Windows.DependencyObject element, System.Windows.IInputElement value);
static member SetFocusedElement : System.Windows.DependencyObject * System.Windows.IInputElement -> unit
Public Shared Sub SetFocusedElement (element As DependencyObject, value As IInputElement)

参数

element
DependencyObject

焦点范围,将使指定元素成为该范围内的 FocusedElementThe focus scope in which to make the specified element the FocusedElement.

value
IInputElement

要获得逻辑焦点的元素。The element to give logical focus to.

示例

下面的示例使用来设置具有逻辑焦点的元素 SetFocusedElement ,并使用来获取具有逻辑焦点的元素 GetFocusedElementThe following example sets the element with logical focus by using the SetFocusedElement and it gets the element with logical focus by using the GetFocusedElement.

// Sets the focused element in focusScope1
// focusScope1 is a StackPanel.
FocusManager.SetFocusedElement(focusScope1, button2);

// Gets the focused element for focusScope 1
IInputElement focusedElement = FocusManager.GetFocusedElement(focusScope1);
' Sets the focused element in focusScope1
' focusScope1 is a StackPanel.
FocusManager.SetFocusedElement(focusScope1, button2)

' Gets the focused element for focusScope 1
Dim focusedElement As IInputElement = FocusManager.GetFocusedElement(focusScope1)

注解

FocusedElement是对特定焦点范围具有逻辑焦点的元素。The FocusedElement is the element that has logical focus for the specific focus scope. 此对象不一定具有键盘焦点。This object may or may not have keyboard focus. 键盘焦点指的是接收键盘输入的元素。Keyboard focus refers to the element that receives keyboard input. 有关焦点、键盘焦点和逻辑焦点的详细信息,请参阅 输入概述For more information on focus, keyboard focus, and logical focus, see the Input Overview.

可以指定一个焦点作用域,该作用域是元素所在的焦点作用域的上级。It is possible to specify a focus scope that is an ancestor of the focus scope the element is in. 例如,如果 StackPanel 是焦点范围,其父级 Window 为焦点范围, TextBox 则的子将在 StackPanel Window 调用时将指定为焦点范围 SetFocusedElementFor example, if a StackPanel is a focus scope and its parent Window is a focus scope, a TextBox child of the StackPanel could specify the Window as the focus scope when calling SetFocusedElement. TextBox FocusedElement 对于 Window 焦点范围和 StackPanel 焦点范围,则为。The TextBox is then FocusedElement for both the Window focus scope and the StackPanel focus scope.

SetFocusedElement 将在指定的焦点范围内给指定元素指定逻辑焦点,并尝试为元素键盘焦点。SetFocusedElement will give the specified element logical focus in the specified focus scope and will attempt to give the element keyboard focus.

适用于