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

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

问题描述

我有一个WPF ComboBox ,我想去的开头的项目(例如)e在 ComboBox 当我键入那封信。如何?

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代码:

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


推荐答案

EDIT: 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 自动完成,根据第一个字母查找匹配 - 假设您的源按字母顺序排序,这将会将所选项目移动到(例如)以e开头的部分。

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天全站免登陆