如何通过在 WPF 组合框中键入键盘字母键来选择项目? [英] How to select item by typing a keyboard letter key in WPF combobox?

查看:22
本文介绍了如何通过在 WPF 组合框中键入键盘字母键来选择项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF ComboBox,当我输入该字母时,我想转到 ComboBox 中以(例如)e"开头的项目.怎么样?

I have a WPF ComboBox and I want to go to items that start with (for example) "e" in the ComboBox when I type that letter. How?

我的 XAML 代码:

My XAML code:

<ComboBox ItemsSource="{Binding Roles}" SelectedValuePath="Id"
          ItemTemplate="{StaticResource ComboBoxDisplayName}"
          SelectedItem="{Binding SelectedRole}"
          Width="150"/> 

推荐答案

我猜你有一个看起来像这样的 ItemTemplate:

I'm guessing you have an ItemTemplate that looks a little like this:

<StackPanel>
    <TextBlock Text="{Binding Path=Foo}" />
    <TextBlock Text="{Binding Path=Bar}" />
</StackPanel>

如果您想在 Foo 上搜索,请尝试...

If you want to search on Foo, then try...

<ComboBox IsEditable = "True" TextSearch.TextPath = "Foo" />

默认情况下,ComboBox 具有一种自动完成功能,可根据第一个字母查找匹配项 - 假设您的源按字母顺序排序,这会将所选项目移至(例如)以开头的部分电子".

By default a ComboBox has a kind of autocomplete that finds matches based on first letter - assuming your source is sorted alphabetically this will shift the selected item to the section that (for example) starts with "e".

捕获 KeyDown 以强制打开下拉列表可能很有用,如果您希望多个条目以相同的字母开头.

Catching KeyDown to force the dropdown to open might be useful if you expect several entries starting with the same letter.

这篇关于如何通过在 WPF 组合框中键入键盘字母键来选择项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆