Keyboard.LostKeyboardFocus Evento adjunto

Definición

Se produce cuando un elemento pierde el foco de teclado.Occurs when an element loses keyboard focus.

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

Ejemplos

En el ejemplo siguiente se TextBox crea un y se adjuntan controladores GotKeyboardFocus de eventos para LostKeyboardFocus el evento y el evento.The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. Cuando obtiene TextBox el foco de teclado, se cambia el color TextBox de fondo y se borra el texto de.When the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. TextBlock Cuando pierde el foco de teclado, se cambia el color de fondo y se llama a un método que restablece las variables usadas en el ejemplo.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

Comentarios

Se trata de un evento adjunto.This is an attached event. WPFWPFimplementa los eventos adjuntos como eventos enrutados.implements attached events as routed events. Los eventos adjuntos son XAMLXAML fundamentalmente un concepto de lenguaje para hacer referencia a eventos que se pueden controlar en objetos que no definen WPFWPF ese evento, que se expande al habilitar también el evento para atravesar una ruta.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. Los eventos adjuntos no tienen una sintaxis de control directo en el código; para adjuntar controladores para un evento enrutado en el código, se usa un método de controlador Add * 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 obtener más información, vea información general sobre eventosadjuntos.For details, see Attached Events Overview.

El foco de teclado hace referencia al objeto que recibe la entrada del teclado.Keyboard focus refers to the object that is receiving keyboard input. El elemento con el foco de IsKeyboardFocused teclado tiene trueestablecido en.The element with keyboard focus has IsKeyboardFocused set to true. Solo puede haber un elemento con el foco de teclado en todo el escritorio.There can be only one element with keyboard focus on the entire desktop. El foco lógico hace referencia al objeto dentro de un ámbito de foco que tiene el foco.Logical focus refers to the object within a focus scope that has focus. Para obtener más información sobre el foco, el foco de teclado y el foco lógico, vea Introducción a la entrada e información general sobre el foco.For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

Si se PreviewGotKeyboardFocus controla el evento PreviewLostKeyboardFocus o el evento, el foco de teclado no cambia.If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus does not change.

Información sobre eventos enrutadosRouted Event Information

Campo de identificadorIdentifier field LostKeyboardFocusEvent
Estrategia de enrutamientoRouting strategy PropagaciónBubbling
delegadoDelegate KeyboardFocusChangedEventHandler

Se aplica a