Keyboard.LostKeyboardFocus Attached Event

Definition

Ocorre quando um elemento perde o foco do teclado.Occurs when an element loses keyboard focus.

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

Examples

O exemplo a seguir cria um TextBox e anexa manipuladores de eventos para o evento GotKeyboardFocus e o evento LostKeyboardFocus.The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. Quando o TextBox Obtém o foco do teclado, a cor do plano de fundo é alterada e o texto da TextBox é limpo.When the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. Quando o TextBlock perde o foco do teclado, a cor do plano de fundo é alterada e um método chamado que redefine as variáveis usadas no exemplo.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

Remarks

Este é um evento anexado.This is an attached event. WPFWPF implementa eventos anexados como eventos roteados.implements attached events as routed events. Os eventos anexados são fundamentalmente um conceito de linguagem XAMLXAML para referenciar eventos que podem ser tratados em objetos que não definem esse evento, que WPFWPF se expande também habilitando o evento para atravessar uma rota.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. Eventos anexados não têm uma sintaxe de manipulação direta no código; para anexar manipuladores para um evento roteado no código, você usa um método manipulador de adição * designado.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. Para obter detalhes, consulte visão geral de eventos anexados.For details, see Attached Events Overview.

O foco do teclado refere-se ao objeto que está recebendo entrada do teclado.Keyboard focus refers to the object that is receiving keyboard input. O elemento com o foco do teclado tem IsKeyboardFocused definido como true.The element with keyboard focus has IsKeyboardFocused set to true. Pode haver apenas um elemento com foco de teclado em toda a área de trabalho.There can be only one element with keyboard focus on the entire desktop. Foco lógico refere-se ao objeto dentro de um escopo de foco que tem foco.Logical focus refers to the object within a focus scope that has focus. Para obter mais informações sobre o foco, o foco do teclado e o foco lógico, consulte visão geral da entrada e visão geral do foco.For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

Se o evento PreviewGotKeyboardFocus ou o evento PreviewLostKeyboardFocus for manipulado, o foco do teclado não será alterado.If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus does not change.

Informações Sobre Eventos RoteadosRouted Event Information

Campo de identificadorIdentifier field LostKeyboardFocusEvent
Estratégia de roteamentoRouting strategy PropagaçãoBubbling
DelegadoDelegate KeyboardFocusChangedEventHandler

Applies to