14

What is the difference(s) between GotFocus and GotKeyboardFocus -and similarly LostFocus and LostKeyboardFocus?

Sorry for the simple question, but, I googled it and read a lot of blog posts, but I'm still confused. It seems nobody knows exactly what is the difference ):

UPDATE:

My usage:

I am creating a custom control by extending Control class. Something like ComboBox but with some other effects. I'm trying to open and close a Popup by setting a property: IsDropDownOpen just like a ComboBox through the GotFocus and LostFocus events. I don't want to Popup get closed, when I Alt+Tabed the windows, but get closed when I click on a Button for example or I go to a TextBox. I did:

private static void OnGotFocusHandler(object sender, RoutedEventArgs e) {
    if (e.Handled)
        return;
    ((SearchBox)sender).IsDropDownOpen = true;
    e.Handled = true;
}

private static void OnLostFocusHandler(object sender, RoutedEventArgs e) {
    if (e.Handled)
        return;
    ((SearchBox)sender).IsDropDownOpen = false;
    e.Handled = true;
}

The GotFocus works. But the Lost one didn't. If I do the Lost stuff in LostKeyboardFocus then when I Alt+Tab the windows, or Window goes to inactive, then the method get called, while I don't want. How can I solve it?

amiry jd
  • 27,021
  • 30
  • 116
  • 215

1 Answers1

18

MSDN has an overview of focus, but I'll try to explain it here.

WPF has 2 concepts regarding focus. There is the physical keyboard focus, and there is logical focus. Only one element can have keyboard focus (and if the application isn't the active application, no element will have keyboard focus).

Multiple items can have logical focus. In fact, you can create new "focus scopes". As per MSDN:

When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

You can define your own focus scope on an element (typically a Panel) by setting FocusManager.IsFocusScope="True". The controls in WPF that are focus scopes by default are Window, MenuItem, ToolBar, and ContextMenu.

This makes sense if you think about having multiple Windows in your application. When you Alt-Tab between them, you expect your keyboard focus to return to the same place it was the last time the Window had focus. By keeping keyboard focus and logical focus separate, you can achieve this.

Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • 1
    +1 thanks, good explanation. actually better than MSDNs'. Thanks a lot. I wait for more answers a while, and then -if no better one received- accepts yours. – amiry jd Aug 09 '13 at 18:09
  • Well, I'm trying to open a `Popup` (in a custom control inherited from `Control` class) by setting a `IsDropDownOpen` property. I set it to `true` in `GotFocus`, it works. I want to set it to `false`, in `LostFocus` event. But it doesn't work. I don't want to do it in `LostKeyboardFocus`, but in `LostFocus`. Any idea please? – amiry jd Aug 09 '13 at 19:49
  • I think you're going to need to create a new question with some code examples. I can't debug your scenario from that brief description. – Abe Heidebrecht Aug 09 '13 at 20:25
  • @king.net: Did you end up creating a question about your particular problem? I *might* be having a related problem, and I'd like to read it and its answers. – O. R. Mapper Aug 31 '14 at 08:31