Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
UIElement Class
UIElement Events
 GotFocus Event
Other versions are also available for the following:
.NET Framework Class Library for Silverlight
UIElement..::.GotFocus Event
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Occurs when a UIElement receives focus.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Visual Basic (Declaration)
Public Event GotFocus As RoutedEventHandler
Visual Basic (Usage)
Dim instance As UIElement
Dim handler As RoutedEventHandler

AddHandler instance.GotFocus, handler
C#
public event RoutedEventHandler GotFocus
Visual C++
public:
 event RoutedEventHandler^ GotFocus {
    void add (RoutedEventHandler^ value);
    void remove (RoutedEventHandler^ value);
}
JScript
JScript does not support events.
XAML Attribute Usage
<object GotFocus="eventhandler"/>

In Silverlight version 1.0, only the root of the object tree was focusable. In Silverlight version 2, any UIElement is focusable.

The GotFocus event is a bubbling event. This means that if multiple GotFocus event handlers are registered for a sequence of objects connected by parent-child relationships in the object tree, the event is received by each object in that relationship. The event is raised by the object that directly receives the input condition that initiates the event, and then bubbles to each successive parent object. The bubbling metaphor indicates that the event starts at the bottom and works its way up the object tree. For a bubbling event, the sender available to the event handler identifies the object where the event is handled, not necessarily the object that actually received the input condition that initiated the event. To get the object that initiated the event, use the Source value of the event's RoutedEventArgs event data.

When you handle the GotFocus or LostFocus events, there is no Handled property available in the event data class for Silverlight 2 Beta 2, and the event will continue to route upwards through the object tree and invoke all attached handlers. This might not always be desirable for your application's handling strategy for focus events, if you want to handle focus at various levels in your object tree. You might consider one of the following techniques:

  • Use a Boolean flag variable to record focusing state. Set your own "Handled" variable to true from the specific handler call that your design deems has appropriately handled the event. Check this "Handled" variable in further handlers to check whether some other handler has already acted, and if so take no action. At the root of the object tree, handle the focus event at least for the purpose of setting "Handled" back to false to reset the state for the next focus event.

  • Place particular emphasis on checking or using the object values transmitted to the handler by sender and by Source in the event data. You might choose to act only if sender equals Source , or if sender is a particular named object in cases when the same focus event handler is attached to multiple objects, or similar logical operations.

GotFocus is not raised if Focus is called on the object but the object already had focus.

GotFocus is raised asynchronously versus the actual focus shift, because of the design of the Silverlight focus system.

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker