In WPF, is OnLostKeyboardFocus guaranteed to be called after OnGotKeyboardFocus?

I have a code that looks like this:

class MyUserControl : Control { ... protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) { base.OnGotKeyboardFocus(e); StartDoingSomethingRisky() } protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) { base.OnLostKeyboardFocus(e); StopDoingSomethingRisky(); } 

Should I worry that in some cases I can continue the risky operation even after the disappearance of MyUserControl or cover 100% of the cases?

+4
source share
1 answer

The only thing that I see wrong is the fact that you are not checking which element has β€œlost” focus.

From http://msdn.microsoft.com/en-us/library/system.windows.uielement.lostkeyboardfocus.aspx : Since this event uses bubbling routing, an element that has lost focus may be a child element, not an element in which the event handler is actually bound. Check the source in the event data to determine the actual item that has lost focus.

If you do something when a certain element has gained focus and stops happening when it loses focus, you will need to check the arguments passed to find out what is called an event.

Hope that helps

+1
source

Source: https://habr.com/ru/post/1479213/


All Articles