WPF ComboBox Key Index not working when using data template selector
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 =开发者_C百科 (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.
Try the IsTextSearchEnabled (http://khason.net/blog/autocomplete-textbox-in-wpf-well-almost/)
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.
精彩评论