tags:

views:

586

answers:

2

I have a WPF combobox and I want to go to items (for example) start with "e" in combobox. How?

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

Assuming your items are sorted alphabetically, simply setting IsTextSearchEnabled="True" should jump to the items starting with the letter (or letters) you type into the ComboBox.

Here is an example of one of my ComboBoxes, I have simplified the bindings as it's clearly not the important part here...

<ComboBox
  ItemsSource="{Binding MyObjectList}"
  DisplayMemberPath="Description"
  SelectedValuePath="Code"
  IsTextSearchEnabled="True"/>

This works perfectly for selecting a value from the list, however, the search value you type will not display in the TextBox part of the control as I have IsEditable set to false.

If anyone would like to explain why this has been voted down it would be appreciated, I don't see any problem with the answer I've provided and don't see why I deserve to lose reputation when I'm only trying to help (and have provided a reasonable answer!)

TabbyCool
Don't you want IsEditable = true, rather than TextSearchEnabled?
MoominTroll
I can't see TextSearchEnabled?
NetSide
IsTextSearchEnabled is not working!!
NetSide
but it is not working.
NetSide
It works perfectly on my ComboBoxes, it might be because I have IsEditable set to false, I don't want to be able to enter a value that isn't already in the list. Focusing on the control and hitting for example, "M" takes me to the first entry starting with "M". Don't know why yours isn't working, a down vote seems a little harsh though!
TabbyCool
+1 - this shouldn't of been downvoted, especially not twice! Works just as well, especially since now we know the problem is the Template.
MoominTroll
In fact, There is no need to add IsEditable, It is enough to add as below.. IsTextSearchEnabled="True" TextSearch.TextPath="Name"
NetSide
+5  A: 

EDIT: 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>

If you want to search on Foo, then try...

<ComboBox IsEditable = "True" TextSearch.TextPath = "Foo" />

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

Catching KeyDown to force the dropdown to open might be useful if you expect several entries starting with teh same letter.

MoominTroll
IsEditable is not working.
NetSide
Should be :/ Make sure that you at least have the MyCombo.IsDropDownOpen = True fired when you catch a keypress. Again, this isn't useful if you're bound collection isnt alphabetically sorted (since it will just jump to the first word of a given letter it finds, rather than filter them). The code I provided should get you started for a filtering autocompletion, however.
MoominTroll
yes I can edit the combo but it is not finding the data I search for. Just edit is enable. ??
NetSide
And I just want to jump, it is enough for me.
NetSide
Actually I'll just edit my answer a bit. Give me 5.
MoominTroll
<DataTemplate x:Key="ComboBoxDisplayName"> <ContentPresenter Content="{Binding Name}"/> </DataTemplate>
NetSide
Is name a string? Is there any reason you have it in a contentPresenter and not a textblock or something?
MoominTroll
Yes, I think it is about dataTemplate. I will look for it. thanks.
NetSide
It works thanks :)
NetSide
In fact, There is no need to add IsEditable, It is enough to add as below..IsTextSearchEnabled="True" TextSearch.TextPath="Name"
NetSide