Carlo Mendoza
Carlo Mendoza

Reputation: 835

UWP AutoSuggestBox SuggestionChosen fires on selection of first item via keyboard up/down

I just discovered SuggestionChosen fires when the first item in an AutoSuggestBox is highlighted using the keyboard up/down keys.

Effectively, I'm unable to pick anything beyond the first one on a list.

This works well for mouse.

What's the correct approach for keyboard navigation?

private async void searchboxaddpart_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        var dw = (Item)args.SelectedItem;
        ViewModel.NavigationService.Navigate(typeof(ItemDetailPage),
            new Item()
            {
                Id = null,
                Description = dw.desc
            });
    }

Upvotes: 3

Views: 540

Answers (1)

Justin XL
Justin XL

Reputation: 39006

You should be doing the navigation under the QuerySubmitted instead of SuggestionChosen. The latter is generally just for updating the Text on the control. This way, the arrow arrays won't be interrupted.

private void searchboxaddpart_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
    if (args.SelectedItem is Item item)
    {
        sender.Text = item.desc;
    }
}

private void searchboxaddpart_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
    if (args.ChosenSuggestion != null && args.ChosenSuggestion is Item item)
    {
        ViewModel.NavigationService.Navigate(typeof(ItemDetailPage),
            new Item
            {
                Id = null,
                Description = item.desc
            });
    }
}

Upvotes: 5

Related Questions