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

1

6

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?

Fabrication answered 28/5, 2009 at 9:8 Comment(0)
H
20

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

e.g.

<ListBox IsTextSearchEnabled="True" 
         TextSearch.TextPath="CustomObject.StringProperty"/>
Hohenzollern answered 28/5, 2009 at 9:21 Comment(4)
Thanks very much. This worked perfectly. Just to be clear, the TextPath value should be a path (the first part looks like a type name, but in this case the item has a property called 'CustomObject' and that object has a property called 'StringProperty'.)Fabrication
True is the default so its up to you whether you want it to look more verbose or notMottle
Works nicely. Can the TextPath be bound to more than one property?Eriha
@Eriha Yes. You can bind one property to multiple sources using MultiBinding. Just Google it..Divagate

© 2022 - 2024 — McMap. All rights reserved.