-->

How can I find an item in a WPF ListBox by typing?

2019-01-22 16:45发布

问题:

Most list boxes allow you to find items within them by typing the first letters of the displayed text. If the typed letters match multiple items, then you can keep adding letters to narrow the search.

I need to do this in a WPF ListBox. However, the items aren't plain strings -- they're custom objects that I present using a DataTemplate. I'm hoping that there's a way I can provide a path to the string value that should be used for this textual keyboard navigation of the ListBox items.

How is this possible?

回答1:

You could try setting IsTextSearchEnabled to true and using the TextSearch.TextPath attached property.

e.g.

<ListBox IsTextSearchEnabled="True" 
         TextSearch.TextPath="CustomObject.StringProperty"/>