#991 – Specifying which Field Is Used for Finding an Item by Typing

You can normally jump to a particular item in an items control by typing the first few characters of the item.  This works, however, based on the string representation of each bound item (the output of ToString).  In some cases, you want to use a specific field when finding an item by typing text.  You can do this by setting TextSearch.TextPath on the list control.

In the example below, we have a data template that displays last and first names of each actor.  We specify that we want to use the last name as a search path.

        <ListBox Name="lbActors" Margin="15,5" Width="200" Height="220"
                 ItemsSource="{Binding ActorList}"
                 TextSearch.TextPath="LastName">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding LastCommaFirst}"/>
                        <TextBlock Text="{Binding Dates}"
                                   FontStyle="Italic"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

We can now just start typing an actor’s last name in order to jump to that actor.  (E.g. “Crawford”).

991-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #991 – Specifying which Field Is Used for Finding an Item by Typing

  1. Pingback: Specifying which Field Is Used for Finding an Item by Typing | Around computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: