Question

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.

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>

Any ideas are appreciated.

Was it helpful?

Solution

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.

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top