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 類別,例如 StackPanelCanvas,將的預設值 Focusable 設定為 false;因此,若要讓這些物件取得鍵盤焦點, Focusable 必須設定為 true

鍵盤焦點是指接收鍵盤輸入的物件。 具有鍵盤焦點的項目已 IsKeyboardFocused 設定為 true。 整個桌面上只能有一個鍵盤焦點的專案。 邏輯焦點是指焦點範圍內具有焦點的物件。 如需焦點、鍵盤焦點和邏輯焦點的詳細資訊,請參閱 輸入 概觀和 焦點概觀

具有鍵盤焦點的專案也有元素所屬焦點範圍的邏輯焦點。 具有邏輯焦點的專案可能或可能沒有鍵盤焦點。

適用於