Keyboard.Focus(IInputElement) 方法

定义

对指定元素设置键盘焦点。Sets keyboard focus on the specified element.

public:
 static System::Windows::IInputElement ^ Focus(System::Windows::IInputElement ^ element);
public static System.Windows.IInputElement Focus (System.Windows.IInputElement element);
static member Focus : System.Windows.IInputElement -> System.Windows.IInputElement
Public Shared Function Focus (element As IInputElement) As IInputElement

参数

element
IInputElement

要对其设置键盘焦点的元素。The element on which to set keyboard focus.

返回

IInputElement

具有键盘焦点的元素。The element with keyboard focus.

示例

下面的示例演示 Loaded 将键盘焦点设置在上的事件处理程序 ButtonThe following example shows a Loaded event handler that sets keyboard focus on a Button.

private void OnLoaded(object sender, RoutedEventArgs e)
{
    // Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton);
}
Private Sub OnLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton)
End Sub

注解

对于用于接收键盘输入的元素,该元素必须可获得焦点。For an element to receive keyboard input, the element must be focusable. 若要使元素可设定焦点,请将 Focusable 基元素上的属性设置为 trueTo make an element focusable, set the Focusable property on the base element to true. 有关基元素的详细信息,请参阅 基元素概述For more information on the base elements, see Base Elements Overview. Panel 类(如 StackPanelCanvas )将的默认值设置 Focusablefalse ; 因此,若要使这些对象获取键盘焦点,则必须将 Focusable 设置为 truePanel classes, such as StackPanel and Canvas, set the default value of Focusable to false; therefore, for these objects to obtain keyboard focus, Focusable must be set to true.

键盘焦点指的是接收键盘输入的对象。Keyboard focus refers to the object that is receiving keyboard input. 具有键盘焦点的元素已 IsKeyboardFocused 设置为 trueThe element with keyboard focus has IsKeyboardFocused set to true. 整个桌面上只能有一个具有键盘焦点的元素。There can be only one element with keyboard focus on the entire desktop. 逻辑焦点指焦点范围内的对象。Logical focus refers to the object within a focus scope that has focus. 有关焦点、键盘焦点和逻辑焦点的详细信息,请参阅 输入概述焦点概述For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

具有键盘焦点的元素也对该元素所属的焦点范围具有逻辑焦点。An element with keyboard focus also has logical focus for the focus scope the element belongs to. 具有逻辑焦点的元素可以有,也可以不具有键盘焦点。An element with logical focus may or may not have keyboard focus.

适用于