Экспорт (0) Печать
Развернуть все
EN
Данное содержимое не доступно на вашем языке, используйте версию на английском языке.

UIElement.LostFocus Event

July 28, 2014

Occurs when a UIElement loses focus.

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

public event RoutedEventHandler LostFocus
<uiElement LostFocus="eventhandler"/>

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 Windows Phone 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 more information, see OnLostFocus.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Показ:
© 2014 Microsoft