Export (0) Print
Expand All

Keyboard.LostKeyboardFocus Attached Event

Occurs when an element loses keyboard focus.

Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

See Keyboard.AddLostKeyboardFocusHandler, Keyboard.RemoveLostKeyboardFocusHandler
See Keyboard.AddLostKeyboardFocusHandler, Keyboard.RemoveLostKeyboardFocusHandler
See Keyboard.AddLostKeyboardFocusHandler, Keyboard.RemoveLostKeyboardFocusHandler
<object Keyboard.LostKeyboardFocus="KeyboardFocusChangedEventHandler" .../>

Identifier field

LostKeyboardFocusEvent

Routing strategy

Bubbling

Delegate

KeyboardFocusChangedEventHandler

The corresponding tunneling event is PreviewLostKeyboardFocus.

Keyboard.LostKeyboardFocus is an attached event. For more information, see Routed Events Overview.

Keyboard focus refers to the object that is receiving keyboard input. The element with keyboard focus will have IsKeyboardFocused set to true. There can be only one element with keyboard focus on the entire desktop. Logical focus refers to the object within a focus scope that has focus. For more information on focus, keyboard focus, and logical focus, see the Input Overview and the Focus Overview.

If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus will not change.

The following example creates a TextBox and attaches event handlers for the GotKeyboardFocus event and the LostKeyboardFocus event. When the TextBox obtains keyboard focus, the background color is changed and the text of the TextBox is cleared. When the TextBlock loses keyboard focus, the background color is changed and a method is called which resets variables used in the sample. For the full samples, see the Keyboard Key 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 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();
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Community Additions

ADD
Show:
© 2014 Microsoft