如何在自动建议框中使用向下箭头键?

Posted

技术标签:

【中文标题】如何在自动建议框中使用向下箭头键?【英文标题】:How to work with down arrow key in Auto Suggest Box? 【发布时间】:2018-01-04 02:35:33 【问题描述】:

在我的自动建议框中,当我在建议列表中按下向下箭头时,建议列表会立即自动关闭。

我想保持打开列表,直到用户按下 Enter 键。 这是我的 XAML 代码

<AutoSuggestBox x:Name="recipient" KeyUp="recipient_KeyDown" FontSize="18" Height="50" TextChanged="recipient_TextChanged" SuggestionChosen="recipient_SuggestionChosen" x:Uid="recipienttextplaceholder" HorizontalAlignment="Left" Background="White" VerticalAlignment="Center" Margin="30,20,0,0" Style="StaticResource AutoSuggestBoxStyle2">
                    <AutoSuggestBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Binding FirstName" Grid.Column="0" x:Name="firstName" Visibility="Binding FirstName, Converter=StaticResource NullOrWhiteSpaceConverter" TextAlignment="Left"/>

                                <TextBlock Text="|" Grid.Column="0" Margin="0,0,-10,0" Visibility="Binding Visibility,ElementName=middleName" HorizontalAlignment="Right"/>

                                <TextBlock Text="Binding MiddleName" Margin="10,0,0,0" Visibility="Binding MiddleName, Converter=StaticResource NullOrWhiteSpaceConverter" x:Name="middleName" Grid.Column="1" TextAlignment="Left"/>

                                <TextBlock Text="|" Grid.Column="1" Visibility="Binding Visibility,ElementName=lastName" Margin="0,0,-6,0" HorizontalAlignment="Right"/>

                                <TextBlock Text="Binding LastName" Margin="10,0,0,0" x:Name="lastName" Visibility="Binding LastName, Converter=StaticResource NullOrWhiteSpaceConverter" Grid.Column="2" TextAlignment="Left"/>

                                <!--<TextBlock Text="|" Grid.Column="2" Margin="0,0,-6,0" Visibility="Binding Visibility,ElementName=lastName" HorizontalAlignment="Right"/>-->
                            </Grid>
                        </DataTemplate>
                    </AutoSuggestBox.ItemTemplate>
                </AutoSuggestBox>

TextChanged 事件:

private void recipient_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    
        if (sender.Text.Length == 0)
        
            recipient.ItemsSource = new List<string>();
            return;
        
        var recipientFilteredByPersonData = new List<Table_People>();
        var recipientFilteredByPlace = new List<Table_Places>();

        if (PeopleList != null)
        
            foreach (var item in PeopleList)
            
                if (item.FirstName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.MiddleName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.LastName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase))
                
                    item.Icon = "../Assets/user_profile.png";
                    recipientFilteredByPersonData.Add(item);
                
            
            cvrbyperson.Source = recipientFilteredByPersonData.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]).ToList();
            //recipient.ItemsSource = recipientFilteredByPersonData;
        

        if (PlacesList != null)
        
            foreach (var item in PlacesList)
            
                if (item.AccountNumber.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.Name.Contains(sender.Text, StringComparison.OrdinalIgnoreCase))
                
                    item.Icon = "../Assets/ic_location.png";
                    recipientFilteredByPlace.Add(item);
                
            
            cvrbyplace.Source = recipientFilteredByPlace.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]).ToList();
            //recipient.ItemsSource = recipientFilteredByPlace;
        
        List<object> combinePlacePerson = (from x in recipientFilteredByPersonData select (object)x).ToList();
        combinePlacePerson.AddRange((from x in recipientFilteredByPlace select (object)x).ToList());
        if (combinePlacePerson.Count == 0)
        
            var noResults = new List<Table_People>();
            var resultWithNoItem = new Table_People  FirstName = "No results." ;
            noResults.Add(resultWithNoItem);
            recipient.ItemsSource = noResults;
        
        else
        
            recipient.ItemsSource = combinePlacePerson;
        
    

建议选择事件:

 private void recipient_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    
        var getType = args.SelectedItem.GetType();
        if (getType.Name == "Table_People")
        
            var selectedItemRecipient = args.SelectedItem as Table_People;
            sender.Text = selectedItemRecipient.FirstName + " " + selectedItemRecipient.LastName;
            recipienterror.Visibility = Visibility.Collapsed;
            _personID = selectedItemRecipient.PersonID;
        
        else
        
            var selectedItemRecipientPlaces = args.SelectedItem as Table_Places;
            _placeID = selectedItemRecipientPlaces.PlaceID;
            sender.Text = selectedItemRecipientPlaces.FirstName + " " + selectedItemRecipientPlaces.LastName;
            recipienterror.Visibility = Visibility.Collapsed;
        

    

我正在从代码中设置 ItemsSource。我需要处理 KeUp 事件吗?

private void recipient_KeyUp(object sender, KeyRoutedEventArgs e)
    
        if (e.Key == Windows.System.VirtualKey.Down)
        
            recipient.IsSuggestionListOpen = true;
        
    

提前致谢。

【问题讨论】:

按下向下键时哪个控件具有焦点,AutoSuggestBox 还是建议列表? 建议列表。 无法重现您的问题,使用AutoSuggestBox 创建了一个简单的应用程序。当焦点切换到建议列表时,向上和向下键都可以工作,无需任何额外的键盘处理代码。您能否发布代码显示您如何打开建议列表? 我在 TextChanged 事件上设置控件的 ItemsSource。 你是如何处理SuggestionChosen事件的? 【参考方案1】:

这可能对你有帮助。我在 Windows 窗体上使用它ComboBox

private void comboBox1_KeyDown(object sender, KeyEventArgs e)

    if (e.KeyCode == Keys.Down) MessageBox.Show("hello");

【讨论】:

这不是winforms。

以上是关于如何在自动建议框中使用向下箭头键?的主要内容,如果未能解决你的问题,请参考以下文章

在组合框中使用向下/向上箭头键选择数据而不更新数据,直到点击选项卡或输入 MS Access

如何在引导下拉菜单中启用向上和向下箭头键

如何在extjs组合框中按Enter键获取所选结果的值

想要文本框中的向上和向下箭头来更改列表框选择

如何在 Angular 9 中使用向上、向下、向左和向右箭头键导航动态输入和选择表内的字段?

为什么h,j,k,l建议使用箭头键为vim?