Keyboard.GotKeyboardFocus Evento anexado

Definição

Ocorre quando um elemento recebe o foco do teclado.

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

Exemplos

O exemplo a seguir cria um TextBox e anexa manipuladores de eventos para o GotKeyboardFocus evento e o LostKeyboardFocus evento . Quando o obtém o TextBox foco do teclado, a cor da tela de fundo é alterada e o texto do TextBox é limpo. Quando o perde o foco do TextBlock teclado, a cor da tela de fundo é alterada e um método é chamado, o que redefine as variáveis usadas no exemplo.

<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

Comentários

Este é um evento anexado. O WPF implementa eventos anexados como eventos roteados. Os eventos anexados são fundamentalmente um conceito de linguagem XAML para referenciar eventos que podem ser manipulados em objetos que não definem esse evento, que o WPF expande também permitindo que o evento percorra uma rota. Os 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, use um método Add*Handler designado. Para obter detalhes, consulte Visão geral de eventos anexados.

O foco do teclado refere-se ao objeto que está recebendo entrada de teclado. O elemento com foco no teclado foi IsKeyboardFocused definido truecomo . Pode haver apenas um elemento com foco no teclado em toda a área de trabalho. O foco lógico refere-se ao objeto dentro de um escopo de foco que tem foco. Para obter mais informações sobre foco, foco do teclado e foco lógico, consulte Visão geral de entrada e Visão geral do foco.

Se o PreviewGotKeyboardFocus evento ou o evento for tratado, o PreviewLostKeyboardFocus foco do teclado será alterado.

Informações de evento encaminhado

Campo Identificador GotKeyboardFocusEvent
Estratégia de roteamento Borbulhando
Delegar KeyboardFocusChangedEventHandler

Aplica-se a