Export (0) Print
Expand All

KeyboardFocusChangedEventArgs Class

Provides data for UIElement.LostKeyboardFocus and UIElement.GotKeyboardFocus routed events, as well as related attached and Preview events.

System.Object
  System.EventArgs
    System.Windows.RoutedEventArgs
      System.Windows.Input.InputEventArgs
        System.Windows.Input.KeyboardEventArgs
          System.Windows.Input.KeyboardFocusChangedEventArgs

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)

public class KeyboardFocusChangedEventArgs : KeyboardEventArgs

The KeyboardFocusChangedEventArgs type exposes the following members.

  NameDescription
Public methodKeyboardFocusChangedEventArgsInitializes a new instance of the KeyboardFocusChangedEventArgs class.
Top

  NameDescription
Public propertyDeviceGets the input device that initiated this event. (Inherited from InputEventArgs.)
Public propertyHandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. (Inherited from RoutedEventArgs.)
Public propertyKeyboardDeviceGets the keyboard device associated with the input event. (Inherited from KeyboardEventArgs.)
Public propertyNewFocusGets the element that focus has moved to.
Public propertyOldFocusGets the element that previously had focus.
Public propertyOriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. (Inherited from RoutedEventArgs.)
Public propertyRoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. (Inherited from RoutedEventArgs.)
Public propertySourceGets or sets a reference to the object that raised the event. (Inherited from RoutedEventArgs.)
Public propertyTimestampGets the time when this event occurred. (Inherited from InputEventArgs.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodInvokeEventHandlerInvokes event handlers in a type-specific way, which can increase event system efficiency. (Overrides KeyboardEventArgs.InvokeEventHandler(Delegate, Object).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnSetSourceWhen overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. (Inherited from RoutedEventArgs.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

This event data class is used with the following attached events:

This event data class is also used with the following routed events on base elements. These routed events forward the previously listed attached events to make them more accessible to the general element model in WPF.

The attached events and the base element routed events share their event data, and the bubbling and tunneling versions of the routed events also share event data. This can affect the handled characteristics of the event as it travels the event route. For details, see Input Overview.

If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled (Handled is set to true in the event data), keyboard focus will not change.

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

This example shows how to change the color of an element when it gains and loses focus by using the GotFocus and LostFocus events.

This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file.

The following XAML creates the user interface, which consists of two Button objects, and attaches event handlers for the GotFocus and LostFocus events to the Button objects.

<StackPanel>
  <StackPanel.Resources>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Height" Value="20"/>
      <Setter Property="Width" Value="250"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
  </StackPanel.Resources>
  <Button
      GotFocus="OnGotFocusHandler"
      LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyboard Focus</Button>
  <Button
      GotFocus="OnGotFocusHandler"
      LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyborad Focus</Button>
</StackPanel>

The following code behind creates the GotFocus and LostFocus event handlers. When the Button gains keyboard focus, the Background of the Button is changed to red. When the Button loses keyboard focus, the Background of the Button is changed back to white.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    // Raised when Button gains focus. 
    // Changes the color of the Button to Red. 
    private void OnGotFocusHandler(object sender, RoutedEventArgs e)
    {
        Button tb = e.Source as Button;
        tb.Background = Brushes.Red;
    }
    // Raised when Button losses focus.  
    // Changes the color of the Button back to white. 
    private void OnLostFocusHandler(object sender, RoutedEventArgs e)
    {
        Button tb = e.Source as Button;
        tb.Background = Brushes.White;
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft