hendch
hendch

Reputation: 41

UWP AutoSuggestBox - Up/Down arrow key fires SuggestionChosen

When the suggestion list is open, the up/down arrow keys automatically fires the SuggestionChosen event. I am wondering if there is a way to intercept this key press? I have tried to use the KeyDown and KeyUp events to catch these key presses but the SuggestionChosen event occurs before the KeyDown/Up events. This effectively forces the user to choose either the first or the last suggestion on the list. Mouse click or touch selection is fine.

I would just like to ignore the arrow keys while typing in the AutoSuggestBox. Or, not force the user to choose the first or last items with the arrow keys. Is there any way to accomplish this? Thank you

XAML

<AutoSuggestBox Name="EmailSuggestBox" 
                PlaceholderText="Email"
                Text="{Binding Customer.EmailAddress, Mode=TwoWay}"
                TextChanged="EmailSuggestBox_TextChanged"
                QuerySubmitted="EmailSuggestBox_QuerySubmitted"
                SuggestionChosen="EmailSuggestBox_SuggestionChosen"
                LostFocus="EmailSuggestBox_LostFocus"
                KeyUp="EmailSuggestBox_KeyUp"
                KeyDown="EmailSuggestBox_KeyDown" />

Methods (Note: vm.EmailOptions is just a list of email domain suggestions)

    private void EmailSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        try
        {
            if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
            {
                if (sender.Text.Contains('@'))
                {
                    var vm = this.DataContext as ProspectInformationEntryViewModel;
                    var d = sender.Text.Split('@');
                    var domain = d.LastOrDefault();
                    List<String> _emailSuggestion = vm.EmailOptions.Where(x => x.StartsWith(domain)).ToList();
                    sender.ItemsSource = _emailSuggestion;
                }
            }
        }
        catch (Exception)
        { }

    }
    private void EmailSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
    {
        try
        {
            if (args.ChosenSuggestion != null)
            {
                sender.ItemsSource = null;
            }
        }
        catch (Exception)
        { }
    }

    private void EmailSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        try
        {
            var domain = args.SelectedItem.ToString();
            var temp = sender.Text.Split('@');
            var identifier = temp.FirstOrDefault();
            sender.Text = identifier + "@" + domain;
            sender.ItemsSource = null;
        }
        catch (Exception)
        { }
    }
    private void EmailSuggestBox_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }

    private void EmailSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Up)
        {
            e.Handled = true;
        }
    }

Upvotes: 4

Views: 768

Answers (2)

kaycee
kaycee

Reputation: 1309

Override the ToString() on your object will do the trick

Upvotes: 1

Nico Zhu
Nico Zhu

Reputation: 32775

I would just like to ignore the arrow keys while typing in the AutoSuggestBox.

For your requirement, you could use ProcessKeyboardAccelerators event to intercept keydown or keyup press.

private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

    if (args.Key == VirtualKey.Down || args.Key == VirtualKey.Up)
    {
        args.Handled = true;
    }
}

Upvotes: 3

Related Questions