Keyboard.LostKeyboardFocus Ansluten händelse

Definition

Inträffar när ett element förlorar tangentbordsfokus.

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

Exempel

I följande exempel skapas en TextBox och kopplas händelsehanterare för GotKeyboardFocus händelsen och händelsen LostKeyboardFocus . TextBox När tangentbordsfokus hämtas ändras bakgrundsfärgen och texten i TextBox den rensas. När tangentbordsfokuset TextBlock förloras ändras bakgrundsfärgen och en metod kallas för att återställa variabler som används i exemplet.

<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

Kommentarer

Det här är en bifogad händelse. WPF implementerar kopplade händelser som dirigerade händelser. Kopplade händelser är i grunden ett XAML-språkkoncept för att referera till händelser som kan hanteras på objekt som inte definierar händelsen, vilket WPF expanderar genom att även aktivera händelsen för att korsa en väg. Kopplade händelser har inte någon syntax för direkt hantering i koden. om du vill koppla hanterare för en dirigerad händelse i kod använder du en angiven Add*Handler-metod. Mer information finns i Översikt över bifogade händelser.

Tangentbordsfokus refererar till objektet som tar emot tangentbordsindata. Elementet med tangentbordsfokus har IsKeyboardFocused angetts till true. Det kan bara finnas ett element med tangentbordsfokus på hela skrivbordet. Logiskt fokus refererar till objektet inom ett fokusomfång som har fokus. Mer information om fokus, tangentbordsfokus och logiskt fokus finns i Översikt över indata och fokusöversikt.

Om händelsen PreviewGotKeyboardFocus eller PreviewLostKeyboardFocus händelsen hanteras ändras inte tangentbordsfokus.

Information om dirigerad händelse

Objekt Value
Identifierarfält LostKeyboardFocusEvent
Routningsstrategi Bubblande
Delegera KeyboardFocusChangedEventHandler

Gäller för