Keyboard.GotKeyboardFocus Attached Event
Occurs when an element receives keyboard focus.
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
This is an attached event. WPF implements attached events as routed events. Attached events are fundamentally a XAML language concept for referencing events that can be handled on objects that do not define that event, which WPF expands upon by also enabling the event to traverse a route. Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. For details, see Attached Events Overview.
Keyboard focus refers to the object that is receiving keyboard input. The element with keyboard focus has 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 Input Overview and Focus Overview.
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.
<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>
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.