Partilhar via


UIElement.LostFocus Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when a UIElement loses focus.

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

Syntax

'Declaration
Public Event LostFocus As RoutedEventHandler
public event RoutedEventHandler LostFocus
<uiElement LostFocus="eventhandler"/>

Remarks

The LostFocus event is a bubbling event. This means that if multiple LostFocus 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 bubbling metaphor indicates that the event starts at the object that directly receives the input condition, 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 OriginalSource 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, 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 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 clear state for the next focus event.

  • Place particular emphasis on checking or using the object values transmitted to the handler by sender and by OriginalSource in the event data. You might choose to act only if sender equals OriginalSource , 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 raised asynchronously versus the actual focus shift, because of the design of the Silverlight focus system. To get a synchronous focus value, use FocusManager.

LostFocus and OnLostFocus

Controls that inherit LostFocus can provide handling for the event that acts as handler for all instances, by overriding the OnLostFocus method. As with direct handling of the event, there is no Handled property available, so OnLostFocus cannot be implemented in such a way that it suppresses further handling of the event through the Handled technique. However, OnLostFocus can be useful either for setting visual state, or for public or internal state properties. For instance, ComboBox implements OnLostFocus in order to set the selected item according to user input that is based on focus. For more information, see OnLostFocus.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.