Keyboard Lookup in a ListBox
Y'know how in plain-old Windows ListBox controls, you can type the first few letters of an item, and it'll move the selection to that item for you?
In Windows Presentation Foundation, you lose that behaviour if you assign an ItemTemplate to the ListBox. Because you're no longer just displaying a simple line of text, the ListBox can't know which item you're trying to select when you start typing.
However, I've just discovered a little trick to get that behaviour back when you have an ItemTemplate set. All you have to do is set the DisplayMemberPath property on the ListBox to point to the (text-based) property of the source item that you want to use to look it up.
So let's say you have a list of items with a "Name" property. You can do this:
<ListBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource MyItemCollection}}" ItemTemplate="{StaticResource MyItemTemplate}"/>
... and now you'll be able to type the first few letters of an item's Name to jump straight to it!
The only caveat is that Visual Studio shows a whole bunch of silent exceptions that it's swallowing in the background, with this message:
System.Windows.Data Error: 20 : Both 'ContentTemplate' and 'ContentTemplateSelector' are set; 'ContentTemplateSelector' will be ignored.
It looks harmless, but it concerns me that this is happening. Still - it's worth it for that keyboard lookup!
Comments
# Bob Smith
7/09/2007 7:24 AM
Too Bad it doesn't work
# mabster
7/09/2007 9:41 AM
(Trying to work out whether Bob Smith is a real person or a spambot...)
Er ... Bob? It does work. At least in normal WPF applications. XBAPs don't compile if you try it, but normal apps do. I wouldn't have blogged about it otherwise.
# Bob Smith
8/09/2007 2:13 AM
First of all, sorry for the undeserved snarkiness from someone who is in effect asking for your help.
I get the following hard error message upon execution of the code:
"Cannot set both DisplayMemberPath and ItemTemplate."
Here is my application of your code snippet:
<ListBox x:Name="lbxStates" Style="{DynamicResource ListBoxTemplate}"
Canvas.Left="533.999" Canvas.Top="390.75"
Width="150" Height="150"
ItemContainerStyle="{StaticResource ItemContStyle}"
SelectionMode="Extended"
DisplayMemberPath="Name"
ItemTemplate="{DynamicResource StatesTemplate}"
ItemsSource="{Binding Path=ObservableStates, Source={StaticResource StatesDS}}"/>
Using VS 2005 8.0.50727.762 (SP 050727.7600)
Framework: 2.0.50727
Merci en avance.
# mabster
8/09/2007 11:34 AM
Hi again Bob!
Yes, I have seen that error when I attempted to do exactly the same thing in an XBAP application.
I can't explain why it worked for me in a stand-alone exe project but not in an XBAP. Maybe it's to do with the particular CTP of Cider (for VS2005) I'm using?
# Bob Smith
18/09/2007 4:32 AM
Hey there,
Just FYI, for a listbox, TextSearch.TextPath will work, without any of the error messages, silent or otherwise.
The Cure's lead singer
# mabster
18/09/2007 8:25 AM
Bob, you are a GENIUS!!!
Thanks for this! I should have realised that the folks in Redmond would have thought of something already! :)