wpfui
wpfui copied to clipboard
AutoSuggestBox stays showing all suggestions even there is no text in the field
Normally if I type in some text and the SuggestBox still works fine.
But when I clear all the text, the box will shows all suggestions, it cannot be unfocus by clicking on other controls, and sometimes it shows on top of other windows. This bug happened on the Demo App as well.
I've got the same issue. The moment the textbox is empty, the suggestions pop up and won't go away no matter what, including being shown on top of other windows when your app isn't even in focus.
As a work-around I tried clearing the suggestions when the box is empty and re-adding when not, but the suggestion box still keeps showing, it's just empty..
Then I tried binding IsSuggestionListOpen so I could set it to false when the text is changed, but it turns out, it's already false and the suggestions show even when IsSuggestionListOpen is false.. The getter of IsSuggestionListOpen is also never ever called.
The solution I found is this:
protected override void OnLostFocus(RoutedEventArgs e)
{
IsSuggestionListOpen = false;
base.OnLostFocus(e);
}
Setting the binding property to false on lost focus since its a textbox. In my case I created a subclass as workaround and overrided the method. It could be easily implemented in the actual control.
In order to close the popup even if apps are switched, I also made this:
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
Window parentWindow = Window.GetWindow(this);
parentWindow.Deactivated += ParentWindow_Deactivated;
}
private void ParentWindow_Deactivated(object? sender, EventArgs e)
{
IsSuggestionListOpen = false;
}
So on window deactivated, it closes the popup