Keyboard.GotKeyboardFocus Evento associato

Definizione

Richiamato quando un elemento riceve lo stato attivo dalla tastiera.

see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler
see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler
see AddGotKeyboardFocusHandler, and RemoveGotKeyboardFocusHandler

Esempio

Nell'esempio seguente vengono creati e TextBox associati gestori eventi per l'evento GotKeyboardFocus e l'evento LostKeyboardFocus . Quando ottiene lo TextBox stato attivo della tastiera, il colore di sfondo viene modificato e il testo di TextBox viene cancellato. Quando perde lo TextBlock stato attivo della tastiera, il colore di sfondo viene modificato e viene chiamato un metodo che reimposta le variabili usate nell'esempio.

<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

Commenti

Si tratta di un evento associato. WPF implementa gli eventi associati come eventi indirizzati. Gli eventi associati sono fondamentalmente un concetto di linguaggio XAML per fare riferimento a eventi che possono essere gestiti su oggetti che non definiscono tale evento, su cui WPF si espande anche consentendo all'evento di attraversare una route. Gli eventi associati non hanno una sintassi di gestione diretta nel codice; per collegare gestori per un evento indirizzato nel codice, usare un metodo Add*Handler designato. Per informazioni dettagliate, vedere Panoramica degli eventi associati.

Lo stato attivo della tastiera fa riferimento all'oggetto che riceve l'input da tastiera. L'elemento con lo stato attivo della tastiera è IsKeyboardFocused impostato su true. Può essere presente un solo elemento con lo stato attivo della tastiera sull'intero desktop. Lo stato attivo logico fa riferimento all'oggetto all'interno di un ambito dello stato attivo con stato attivo. Per altre informazioni sullo stato attivo, lo stato attivo della tastiera e lo stato attivo logico, vedere Panoramica dell'input e Panoramica dello stato attivo.

Se l'evento o l'evento PreviewGotKeyboardFocusPreviewLostKeyboardFocus viene gestito, lo stato attivo della tastiera cambia.

Informazioni evento indirizzato

Campo Identificatore GotKeyboardFocusEvent
Strategia di routing Bubbling
Delegato KeyboardFocusChangedEventHandler

Si applica a