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