UWP AutoSuggestBox - 向上/向下箭头键触发SuggestionChosen

4
当建议列表打开时,上下箭头键自动触发SuggestionChosen事件。我想知道是否有一种方法拦截这个按键?我已经尝试使用KeyDown和KeyUp事件来捕捉这些按键,但是SuggestionChosen事件会在KeyDown/Up事件之前发生。这实际上迫使用户选择列表上的第一个或最后一个建议。鼠标单击或触摸选择没有问题。我只想在输入AutoSuggestBox时忽略箭头键。或者不用箭头键强制用户选择第一个或最后一个项目。有没有办法实现这个?谢谢。
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" />

方法(注意:vm.EmailOptions仅是电子邮件域建议列表)

    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;
        }
    }

箭头键是默认行为的一部分,这是人们所期望的。只有在您知道用户不希望它们像往常一样工作时,才应禁用它们。 - NielsNet
1
尝试使用PreviewKeyDown和PreviewKeyUp,它们会在相应的KeyUp和KeyDown事件之前触发。 - Muhammad Touseef
@NielsNet 刚刚编辑了帖子。上/下箭头键会强制用户选择最后一个/第一个项目。我不需要禁用它们,但我希望用户能够从建议列表中选择超过2个选项。 - hendch
@touseefbsb 很不幸,AutoSuggestBox 没有 PreviewKeyDown 和 PreviewKeyUp 事件。 - hendch
我猜这只适用于SDK创建者更新及以上版本。 - Muhammad Touseef
问题在于,基本上突出显示建议会修改文本以匹配建议,因此事件等同于“选择建议”。 - Martin Zikmund
2个回答

3
我希望在使用AutoSuggestBox时,可以忽略箭头键的输入。
对于您的需求,您可以使用ProcessKeyboardAccelerators事件来拦截按下或松开键盘按键。
private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

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

通过这样做,上下箭头完全停止工作,我们无法使用箭头键进入建议列表,我们是否可以在不触发建议选择的情况下使用箭头键进入列表? - Muhammad Touseef

0

重写对象的ToString()方法就可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接