Reputation: 3890
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"/>
Upvotes: 21
Views: 14781
Reputation: 89
I know this is an old post, but this might help someone. If you are using binding, which you should be, along with ItemSource and SelectedItem, then just adding TextSearch.TextPath="your bound display property" as stated by @mwolff works fine.
<ComboBox Grid.Row="2" Grid.Column="0" Height="25" VerticalContentAlignment="Center" HorizontalAlignment="Center" Width="150" ItemsSource="{Binding EquipmentTypes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedEquipmentType,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Equipment_Type_ID"
TextSearch.TextPath="Equipment_Type_Code"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ComboBox.Resources>
<Style TargetType="Popup">
<Setter Property="Width" Value="155"/>
</Style>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Equipment_Type_Code}" TextWrapping="NoWrap" />
</DataTemplate>
</ComboBox.ItemTemplate>
Upvotes: 0
Reputation: 11
All I had to do was add the following:
TextSearch.TextPath="<what ever you bound to goes here> ie:State or name "
Upvotes: 1
Reputation: 2839
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 ComboBox
es, 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!)
Upvotes: 15
Reputation: 2552
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.
Upvotes: 27