gpt4 book ai didi

c# - 您如何根据在此问题中所做的按键字母选择列表框中的项目,但在 UWP 中?

转载 作者:行者123 更新时间:2023-12-03 11:02:14 30 4
gpt4 key购买 nike

How to select item by typing a keyboard letter key in WPF combobox?

我希望能够选择与列表框中按下的键相匹配的项目首字母的第一项,当列表框处于焦点时,A-Z。

<ListBox x:Name="List" ItemContainerStyle="{StaticResource ListBoxItem}" DataContext="{StaticResource VM}" 
ItemsSource="{Binding Names, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" DisplayMemberPath="Name"
Style="{StaticResource ResourceKey=ListBox}"/>

最佳答案

当您绑定(bind) 商品来源 将您的组合框添加到 ViewModel 中的集合中,那么您需要做的就是捕获 按键 ComboBox 上的事件,然后相应地过滤项目,然后选择所需的项目。最后只需使用 StartBringIntoView()在用户面前滚动该项目的方法。

private void MyComboBox_KeyDown(object sender, KeyRoutedEventArgs args)
{
if (e.Key == Windows.System.VirtualKey.E)
{
//Now you need to select the first item which starts with letter E.
//Assuming your combobox's itemssource has a binding to a collection named "MyCollection" then this is how you can achieve it :
var item = MyCollection.First(a=>a.StartsWith("E"));
//Now you can set this item to the SelectedItem property of your combobox or you can get its index in the collection and then set SelectedIndex of your combobox.
var index = MyCollection.IndexOf(item);
MyComboBox.SelectedIndex = index;//now you have selected the desired item
//LastStep is to bring that selected item into view of the user.
MyComboBox.SelectedItem.StartBringIntoView();
}
}

请注意 StartBringIntoView 仅在 Windows 10 SDK 创建者更新及更高版本中可用。

关于c# - 您如何根据在此问题中所做的按键字母选择列表框中的项目,但在 UWP 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52487714/

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