C# UWP AutoSuggestBox-上/下箭头键激发SuggestSelected

C# UWP AutoSuggestBox-上/下箭头键激发SuggestSelected,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,当建议列表打开时,上/下箭头键会自动触发SuggestionSelected事件。我想知道是否有办法截获这个按键?我尝试使用KeyDown和KeyUp事件捕捉这些按键,但SuggestionSelected事件发生在KeyDown/Up事件之前。这有效地迫使用户选择列表中的第一个或最后一个建议。鼠标单击或触摸选择即可 我只想在AutoSuggestBox中键入时忽略箭头键。或者,不要强制用户使用箭头键选择第一个或最后一个项目。有没有办法做到这一点?多谢各位 XAML 方法(注意:vm.Ema

当建议列表打开时,上/下箭头键会自动触发SuggestionSelected事件。我想知道是否有办法截获这个按键?我尝试使用KeyDown和KeyUp事件捕捉这些按键,但SuggestionSelected事件发生在KeyDown/Up事件之前。这有效地迫使用户选择列表中的第一个或最后一个建议。鼠标单击或触摸选择即可

我只想在AutoSuggestBox中键入时忽略箭头键。或者,不要强制用户使用箭头键选择第一个或最后一个项目。有没有办法做到这一点?多谢各位

XAML


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

private void EmailSuggestBox\u text已更改(AutoSuggestBox发件人、AutoSuggestBoxTextChangedEventArgs args)
{
尝试
{
if(args.Reason==AutoSuggestionBoxTextChangeReason.UserInput)
{
if(sender.Text.Contains('@'))
{
var vm=this.DataContext作为ProspectInformationEntryViewModel;
var d=sender.Text.Split('@');
var domain=d.LastOrDefault();
List_emailSuggestion=vm.EmailOptions.Where(x=>x.StartsWith(domain)).ToList();
sender.ItemsSource=\u电子邮件建议;
}
}
}
捕获(例外)
{ }
}
私有无效EmailSuggestBox_QuerySubmitted(AutoSuggestBox发件人,AutoSuggestBoxQuerySubmittedEventArgs)
{
尝试
{
if(args.ChosenSuggestion!=null)
{
sender.ItemsSource=null;
}
}
捕获(例外)
{ }
}
private void EmailSuggestBox_SuggestionSelected(AutoSuggestBox发件人、AutoSuggestBoxSuggestionChosenEventArgs args args)
{
尝试
{
var domain=args.SelectedItem.ToString();
var temp=sender.Text.Split('@');
var identifier=temp.FirstOrDefault();
sender.Text=标识符+“@”+域;
sender.ItemsSource=null;
}
捕获(例外)
{ }
}
私有无效EmailSuggestBox_KeyUp(对象发送者,KeyRoutedEventArgs e)
{
if(e.Key==Windows.System.VirtualKey.Down | | e.Key==Windows.System.VirtualKey.Up)
{
e、 已处理=正确;
}
}
私有无效EmailSuggestBox_KeyDown(对象发送者,KeyRoutedEventArgs e)
{
if(e.Key==Windows.System.VirtualKey.Down | | e.Key==Windows.System.VirtualKey.Up)
{
e、 已处理=正确;
}
}
我只想在AutoSuggestBox中键入时忽略箭头键

根据您的需求,您可以使用事件来截取keydown或keydup按键

private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

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

箭头键是我们所期望的默认行为的一部分。只有当您知道您的用户不希望他们使用PreviewKeyDown和PreviewKeyUp作为高利贷时,才应该禁用它们。相反,这两个选项分别在KeyUp和KeyDown之前触发。@NielsNet刚刚编辑了这篇文章。上/下箭头键强制用户分别选择最后/第一项。我不需要禁用它们,但我希望用户能够从建议列表中的两个以上选项中进行选择。@ToSeefbsb不幸的是,AutoSuggestBox没有PreviewKeyDown和PreviewKeyUp事件。我猜它仅适用于sdk创建者更新和更高版本。这样做完全停止工作,我们无法在都有箭头键,我们可以在没有选择的情况下用箭头键进入列表吗?
    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;
        }
    }
private void Autosbox_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{

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