views:

14

answers:

2

Hi all,

I have a problem regarding WPF combobox when using data template selector.

Basically, WPF combobox has a standard behaviour which will take you directly to the item that starts from a character if you type that character using keyboard. I don't know the official name for this functionality and will temporarily call it "Key Indexing".

I now want to create a ComboBox which displays its item differently. I achieved this using DataTemplateSelector;

    <ComboBox SelectedItem="{Binding Selection}" x:Name="Input" ItemsSource="{Binding Parties}">
        <ComboBox.ItemTemplateSelector>
            <Editor:PartyTemplateSelector DefaultTemplate="{StaticResource Default}" NewTemplate="{StaticResource New}" OldTemplate="{StaticResource Old}"/>
        </ComboBox.ItemTemplateSelector>
    </ComboBox>

And the PartyTemplateSelector is:

public class PartyTemplateSelector : DataTemplateSelector
{
    public DataTemplate DefaultTemplate
    {
        get; set;
    }

    public DataTemplate NewTemplate
    {
        get; set;
    }

    public DataTemplate OldTemplate
    {
        get; set;
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var party = (Party) item;
        if (party is OldDisplay)
        {
            return OldTemplate;                
        }

        if(counterparty.NewLook)
        {
            return NewTemplate;
        }
        return DefaultTemplate;
    }
}

It works fine apart from that the key indexing ability is lost. When I type a key while the combobox is dropped down, it does not take me to the item that starts from the character I typed.

Can anyone help me with this?

Regards.

A: 

Try the IsTextSearchEnabled (http://khason.net/blog/autocomplete-textbox-in-wpf-well-almost/)

rudigrobler
Hi, Thanks for your reply. This value has its default value true and does not need to be set explicitly. I have tried but it does not solve the problem. :( Many thanks anyway.
Kevin Zhang
A: 

Try <TextSearch.TextPath="prop" /> where 'prop' should be the path to the property you want to examine for matches against the keypresses.

Edit: of course, you can use it directly inside the ComboBox tag and not as a separate one.

Alex Paven