【问题标题】:UWP AutoSuggestBox - Up/Down arrow key fires SuggestionChosenUWP AutoSuggestBox - 向上/向下箭头键触发 SuggestionChosen
【发布时间】:2022-01-27 21:24:07
【问题描述】:

当建议列表打开时,向上/向下箭头键会自动触发 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;
        }
    }

【问题讨论】:

  • 箭头键是人们期望的默认行为的一部分。仅当您知道您的用户不希望它们像往常一样工作时,您才应该禁用它们
  • 尝试使用 PreviewKeyDown 和 PreviewKeyUp 而不是,这两个分别在 KeyUp 和 KeyDown 之前触发。
  • @NielsNet 刚刚编辑过帖子。上/下箭头键强制用户分别选择最后一项/第一项。我不需要禁用它们,但我希望用户能够从建议列表中的 2 个以上选项中进行选择。
  • @touseefbsb 不幸的是,AutoSuggestBox 没有 PreviewKeyDown 和 PreviewKeyUp 事件
  • 我猜它只适用于 sdk creators update 及以上版本。

标签: c# uwp uwp-xaml


【解决方案1】:

我只想在 AutoSuggestBox 中键入时忽略箭头键。

根据您的要求,您可以使用ProcessKeyboardAccelerators 事件来拦截 keydown 或 keyup 按下。

private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

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

【讨论】:

  • 通过向上/向下完全停止工作,我们根本无法使用箭头键进入建议列表,我们可以在不触发建议的情况下使用箭头键进入列表吗?
【解决方案2】:

覆盖对象上的 ToString() 即可解决问题

【讨论】: