Share via


Keyboard.LostKeyboardFocus Kejadian Terlampir

Definisi

Terjadi ketika elemen kehilangan fokus keyboard.

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

Contoh

Contoh berikut membuat TextBox dan melampirkan penanganan aktivitas untuk GotKeyboardFocus peristiwa dan LostKeyboardFocus peristiwa. TextBox Saat mendapatkan fokus keyboard, warna latar belakang diubah dan teks TextBox dikosongkan. TextBlock Ketika kehilangan fokus keyboard, warna latar belakang diubah dan metode disebut yang mengatur ulang variabel yang digunakan dalam sampel.

<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

Keterangan

Ini adalah peristiwa terlampir. WPF menerapkan peristiwa terlampir sebagai peristiwa yang dirutekan. Peristiwa terlampir pada dasarnya adalah konsep bahasa XAML untuk mereferensikan peristiwa yang dapat ditangani pada objek yang tidak menentukan peristiwa tersebut, yang diperluas WPF dengan juga memungkinkan peristiwa untuk melintasi rute. Peristiwa terlampir tidak memiliki sintaks penanganan langsung dalam kode; untuk melampirkan handler untuk peristiwa yang dirutekan dalam kode, Anda menggunakan metode Tambahkan*Handler yang ditunjuk. Untuk detailnya, lihat Gambaran Umum Peristiwa Terlampir.

Fokus keyboard mengacu pada objek yang menerima input keyboard. Elemen dengan fokus keyboard telah IsKeyboardFocused diatur ke true. Hanya ada satu elemen dengan fokus keyboard pada seluruh desktop. Fokus logis mengacu pada objek dalam cakupan fokus yang memiliki fokus. Untuk informasi selengkapnya tentang fokus, fokus keyboard, dan fokus logis, lihat Gambaran Umum Input dan Gambaran Umum Fokus.

PreviewGotKeyboardFocus Jika peristiwa atau PreviewLostKeyboardFocus acara ditangani, fokus keyboard tidak berubah.

Informasi Peristiwa Yang Dirutekan

Bidang pengidentifikasi LostKeyboardFocusEvent
Strategi perutean Menggelegak
Delegasikan KeyboardFocusChangedEventHandler

Berlaku untuk