通过


Keyboard.Focus(IInputElement) 方法

定义

设置指定元素上的键盘焦点。

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

要设置键盘焦点的元素。

返回

具有键盘焦点的元素。

示例

以下示例显示了一个Loaded事件处理程序,用于设置键盘焦点。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

注解

要使元素接收键盘输入,该元素必须可聚焦。 若要使元素可聚焦,请将 Focusable 基元素上的属性设置为 true。 有关基元素的详细信息,请参阅 基本元素概述Panel类(如StackPanel和)将默认值Focusablefalse设置为 ;因此,要使这些对象获取键盘焦点,Focusable必须设置为 trueCanvas

键盘焦点是指接收键盘输入的对象。 具有键盘焦点的元素已 IsKeyboardFocused 设置为 true。 整个桌面上只能有一个具有键盘焦点的元素。 逻辑焦点是指具有焦点的焦点范围内的对象。 有关焦点、键盘焦点和逻辑焦点的详细信息,请参阅 输入概述焦点概述

具有键盘焦点的元素也具有元素所属的焦点范围的逻辑焦点。 具有逻辑焦点的元素可能或可能没有键盘焦点。

适用于