dotnetprojects/WpfToolkit

AutoCompleteBox's LostFocus Event triggers when clicking on its Popup items

Closed this issue · 5 comments

It works perfectly fine with keyboard selection, but if you try to select an item from the Popup list with a mouse click, it will trigger the AutoCompleteBox's LostFocus Event twice, while the control still has focus in the end.

Very simple steps to reproduce :

  • Listen to an AutoCompleteBox's LostFocus Event
  • Click on one of its Popup items

as a workaround I can suggest to use reactive and apply a buffer or a debounce or if you can distinct by some data you can use a distinct

in the meanwhile if you have an idea on how we can fix this problem from the source code I would encourage you to open a pull request

thank you

Not sure if this can be fixed, since it does happen as well on basic controls like ComboBoxes. A solution I found to filter those misleading Events is to check if KeyBoardFocus is still within the control on LostFocus like the following:

private void AutoCompleteBox_LostFocus(object sender, RoutedEventArgs e)
{
    if (!(sender as Control).IsKeyboardFocusWithin)
    { 
        // The Control truly lost focus
    }
}

Not sure if this can be fixed, since it does happen as well on basic controls like ComboBoxes

so I would close the issue, because I think we should not block something that is coming from the under framework.

BTW I think I pointed you out what is a good practice to do with events in general, reactive it's designed a lot for that.

BTW I think I pointed you out what is a good practice to do with events in general, reactive it's designed a lot for that.

I checked the filters you pointed out in the previous comment, but unfortunately none of them seemed to fit for this particular situation. The simple Filter filter could probably do the trick with the IsKeyboardFocusWithin condition, but it's already possible to do that filtering without Reactive, so unless there's a speed difference or some information about Reactive I don't know yet, It doesn't seem necessary in this case.

but it's already possible to do that filtering without Reactive, so unless there's a speed difference or some information about Reactive I don't know yet, It doesn't seem necessary in this case.

in your case you're right, stick on the simplest way, reactive gain a lot when you have a stream of events, and you want a finer control over it