WPF ComboBox使用快捷键来选择项目 [英] WPF ComboBox using shortcut keys for selecting items

查看:924
本文介绍了WPF ComboBox使用快捷键来选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个组合框,使用以下数据模板。在论坛的帮助下,我可以让这个显示和行为正确。

I have a combo box in WPF that uses the following data template. With help from the forum I was able to get this to display and behave properly.

当下拉菜单打开时,我希望用户能够键入一个字母并使下拉菜单跳到该选择。我已经看到这个实现两种方式,其中它基本上捕获组合框中的文本框中的键盘输入,并根据用户键入的内容进行选择。这允许用户等待并键入更多添加到条件的文本。另一个是键盘输入似乎没有捕获到任何地方,如果你输入一个字母,等待几秒钟,并键入另一个字母,它带你到您输入的第二个字母开始的选择。如果您要输入搜索条件的多个字母,您必须一起快速键入它们。

When the drop down is open I would like the user to be able to type a letter and have the drop down skip to that selection. I have seen this implemented two ways, one where it bascially captures the keyboard input in a text box in the combo box and selects based on what the user has typed. This allows the user to wait and type more text that gets add to the criteria. The other is where the keyboard input doesn't appear to get captured anywhere, if yo type a letter, wait a couple seconds and type another letter it takes you to the selection starting with the second letter you typed. If you want to type multiple letters of the search criteria you have to type them quickly together.

我会很高兴任何一种方法。他们只能选择一个项目,虽然是在列表中,所以我不太喜欢在组合框上放置一个编辑控件的想法,他们可以输入任何他们想要的。我可以这样做,而不是验证他们的输入,但宁愿强迫他们选择列表中的东西。

I'd be happy with either approach. They can only select an item though that is in the list, so I don't really like the idea of putting an edit control on the combo box where they can type in whatever they want. I could do this and than validate their input, but would rather force them to select something that is in the list.

在这种情况下,它是系统颜色的列表,其中超过140个,所以有一个方法来快速找到列表是我需要的。

In this case it is a list of system colors, which is over 140 of them, so having a way to quickly get around the list is what I need.

这里是我使用的数据模板。

Here is my data template that I am using.

    <DataTemplate x:Key="ColorSelectionComboBox" DataType="ComboBox">
        <StackPanel Orientation="Horizontal">
            <Rectangle Width="16" Height="16" Margin="0,2,4,2">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Color}"/>
                </Rectangle.Fill>
            </Rectangle>
            <TextBlock Text="{Binding Name}"/>
        </StackPanel>
    </DataTemplate>

任何想法都非常感激。

推荐答案

ComboBox内置支持使用键盘进行增量搜索。我实际上并没有使用(因此我不能帮助进行高级故障排除),但我知道它在那里。

ComboBox has built-in support for incremental search using the keyboard. I haven't actually used it (so I'm no good for help with advanced troubleshooting) but I know it's there.

例如您需要设置ComboBox的 IsTextSearchEnabled 属性设置为true,然后设置 TextSearch。 TextPath 将属性附加到要搜索的文本的绑定路径(可能是名称与您的TextBlock文本相对应)。

It looks like you need to set the ComboBox's IsTextSearchEnabled property to true, and then set the TextSearch.TextPath attached property to the binding path of the text you want to search on (probably "Name" to correspond with your TextBlock text).

这篇关于WPF ComboBox使用快捷键来选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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