Keyboard.LostKeyboardFocus Keyboard.LostKeyboardFocus Keyboard.LostKeyboardFocus Attached Event

定義

當項目失去鍵盤焦點時發生。Occurs when an element loses keyboard focus.

see AddLostKeyboardFocusHandler, and RemoveLostKeyboardFocusHandler
see AddLostKeyboardFocusHandler, and RemoveLostKeyboardFocusHandler
see AddLostKeyboardFocusHandler, and RemoveLostKeyboardFocusHandler

範例

下列範例會建立TextBox並附加事件和LostKeyboardFocus事件的事件GotKeyboardFocus處理常式。The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. 當取得鍵盤焦點時, 背景色彩會變更, 而且TextBox會清除的文字。 TextBoxWhen the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. TextBlock當失去鍵盤焦點時, 背景色彩會變更, 並會呼叫方法, 以重設範例中使用的變數。When the TextBlock loses keyboard focus, the background color is changed and a method is called that resets variables used in the sample.

<Border BorderBrush="Black" BorderThickness="1"
        Width="200" Height="100" Margin="5">
  <StackPanel>
    <Label HorizontalAlignment="Center" Content="Type Text In This TextBox" />
    <TextBox Width="175"
             Height="50" 
             Margin="5"
             TextWrapping="Wrap"
             HorizontalAlignment="Center"
             VerticalScrollBarVisibility="Auto"
             GotKeyboardFocus="TextBoxGotKeyboardFocus"
             LostKeyboardFocus="TextBoxLostKeyboardFocus"
             KeyDown="SourceTextKeyDown"/>
  </StackPanel>
</Border>
private void TextBoxGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    TextBox source = e.Source as TextBox;

    if (source != null)
    {
        // Change the TextBox color when it obtains focus.
        source.Background = Brushes.LightBlue;

        // Clear the TextBox.
        source.Clear();
    }
}
Private Sub TextBoxGotKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs)
    Dim source As TextBox = TryCast(e.Source, TextBox)

    If source IsNot Nothing Then
        ' Change the TextBox color when it obtains focus.
        source.Background = Brushes.LightBlue

        ' Clear the TextBox.
        source.Clear()
    End If
End Sub
private void TextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    TextBox source = e.Source as TextBox;

    if (source != null)
    {
        // Change the TextBox color when it loses focus.
        source.Background = Brushes.White;

        // Set the  hit counter back to zero and updates the display.
        this.ResetCounter();
    }
}
Private Sub TextBoxLostKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs)
    Dim source As TextBox = TryCast(e.Source, TextBox)

    If source IsNot Nothing Then
        ' Change the TextBox color when it loses focus.
        source.Background = Brushes.White

        ' Set the  hit counter back to zero and updates the display.
        Me.ResetCounter()
    End If
End Sub

備註

這是附加事件。This is an attached event. WPFWPF將附加事件實作為路由事件。implements attached events as routed events. 附加事件基本上是參考XAMLXAML事件的一種語言概念, 可以在未定義該事件的物件上處理, 這WPFWPF也會讓事件通過路由來進行擴充。Attached events are fundamentally a XAMLXAML language concept for referencing events that can be handled on objects that do not define that event, which WPFWPF expands upon by also enabling the event to traverse a route. 附加事件在程式碼中沒有直接處理語法;若要在程式碼中附加路由事件的處理常式, 您可以使用指定的 Add * 處理常式方法。Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. 如需詳細資訊, 請參閱附加事件總覽For details, see Attached Events Overview.

鍵盤焦點是指接收鍵盤輸入的物件。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.

PreviewGotKeyboardFocus如果事件PreviewLostKeyboardFocus或事件已處理, 則不會變更鍵盤焦點。If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus does not change.

路由事件資訊Routed Event Information

識別碼欄位Identifier field LostKeyboardFocusEvent
路由策略Routing strategy 路由Bubbling
Delegate - 委派Delegate KeyboardFocusChangedEventHandler

適用於