21

I have a WPF ComboBox and I want to go to items that start with (for example) "e" in the ComboBox when I type that letter. How?

My XAML code:

<ComboBox ItemsSource="{Binding Roles}" SelectedValuePath="Id"
          ItemTemplate="{StaticResource ComboBoxDisplayName}"
          SelectedItem="{Binding SelectedRole}"
          Width="150"/> 
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
NetSide
  • 3,849
  • 8
  • 30
  • 41

3 Answers3

26

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 the same letter.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
MoominTroll
  • 2,502
  • 21
  • 24
  • 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 Feb 05 '10 at 12:07
  • 1
    yes I can edit the combo but it is not finding the data I search for. Just edit is enable. ?? – NetSide Feb 05 '10 at 12:10
  • 1
    And I just want to jump, it is enough for me. – NetSide Feb 05 '10 at 12:12
  • 1
  • 1
    Is name a string? Is there any reason you have it in a contentPresenter and not a textblock or something? – MoominTroll Feb 05 '10 at 12:27
  • 1
    Yes, I think it is about dataTemplate. I will look for it. thanks. – NetSide Feb 05 '10 at 13:08
  • 2
    In fact, There is no need to add IsEditable, It is enough to add as below.. IsTextSearchEnabled="True" TextSearch.TextPath="Name" – NetSide Feb 08 '10 at 07:21
  • IsTextSearchEnabled by itslef will only work if your items are strings otherwise if they are custom CLR objects it will call ToString() resulting in something like "MyObject" which is probably not what you want. Use IsEditbale only if you want it to be editable! Otherwise override ToString() or Handel the KeyDown event and do it procedurally – markmnl Sep 26 '11 at 05:41
15

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
TabbyCool
  • 2,829
  • 1
  • 24
  • 36
  • Don't you want IsEditable = true, rather than TextSearchEnabled? – MoominTroll Feb 05 '10 at 10:12
  • 1
    I can't see TextSearchEnabled? – NetSide Feb 05 '10 at 10:32
  • 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 Feb 05 '10 at 14:36
  • 1
    +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 Feb 05 '10 at 23:23
  • In fact, There is no need to add IsEditable, It is enough to add as below.. IsTextSearchEnabled="True" TextSearch.TextPath="Name" – NetSide Feb 08 '10 at 12:49
  • IsTextSearchEnabled calls ToString() on the objects in the Items - if you have a custom object, i.e. not a string, it will not appear to work as will be calling on ToString() which will give something like "MyObject" unless you override the ToString() in your custom object to output say the Code property... – markmnl Sep 26 '11 at 05:39
  • I suspect the down vote is more due to the way you are misusing WPF and using the very "winforms" way of doing it, ie DisplayMemberPath instead of using a DataTemplate. – MikeKulls Nov 21 '11 at 02:51
  • 1
    Using the DisplayMemberPath is a perfectly valid and simple way of doing this, using DataTemplates for everything is just overkill in my opinion. Also, it might be considered the "WinForms" way of doing things, but I'd never even worked with WinForms before doing WPF (unfortunately I'm stuck doing WinForms now though!) – TabbyCool Nov 29 '11 at 15:24
0

All I had to do was add the following:

TextSearch.TextPath="<what ever you bound to goes here> ie:State or name "
iamruss
  • 782
  • 4
  • 11
mwolff
  • 1