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

我想一直打开列表,直到用户按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;
        }
    }


提前致谢。

最佳答案

那可能对您有帮助。我在Windows窗体ComboBox上使用了它

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down) MessageBox.Show("hello");
}

关于c# - 如何使用自动建议框中的向下箭头键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45366456/

10-16 10:50