gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:39 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com