Keyboard.LostKeyboardFocus Keyboard.LostKeyboardFocus Keyboard.LostKeyboardFocus Attached Event

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 crea un TextBox y adjunta los controladores de eventos para el GotKeyboardFocus eventos y el LostKeyboardFocus eventos.The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. Cuando el TextBox recibe el foco de teclado, se cambia el color de fondo y el texto de la TextBox está desactivada.When the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. Cuando el TextBlock pierde el foco de teclado, se cambia el color de fondo y se llama a un método que restablece las variables utilizadas 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. WPFWPF implementa los eventos adjuntos como eventos enrutados.implements attached events as routed events. Los eventos adjuntos son fundamentalmente un XAMLXAML concepto de lenguaje para hacer referencia a los eventos que pueden controlarse en objetos que no definen ese evento, que WPFWPF amplía habilitando también el evento recorrer 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, utilice Agregar designado * método del controlador.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, consulte Attached Events Overview.For details, see Attached Events Overview.

El foco de teclado hace referencia al objeto que recibe la entrada de teclado.Keyboard focus refers to the object that is receiving keyboard input. El elemento con foco de teclado tiene IsKeyboardFocused establecido en true.The element with keyboard focus has IsKeyboardFocused set to true. Puede haber solo un elemento con foco de teclado en todo el escritorio.There can be only one element with keyboard focus on the entire desktop. 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, consulte Input Overview y foco lógico.For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

Si el PreviewGotKeyboardFocus eventos o PreviewLostKeyboardFocus controla el evento, no cambia el foco de teclado.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