Click here to Skip to main content
13,596,798 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 24 Jun 2012
Licenced CPOL

Highlight Searched Text in WPF ListView

, 25 Jun 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Highlight Searched Text in WPF ListView"


An alternative implementation of 's article located here: Highlight-Searched-Text-in-WPF-ListView

This is how to filter + sort thousands of items while also highlighting text which matches your search phrase while also utilizing virtualization so that WPF programs don't run poorer then usual.   

New in this nearly 100% original and rewritten variation is: 

  • The ability to search and filter out items that don't match. 
  • The ability to sort the items by clicking the column header of the listview. 
  • A highly efficient implementation that does not rely on recursion to apply the highlighting effect. 
  • An example of a "WPF Custom Control Class Library"!! (all you need to know is it's just a regular class library, but you need to configure its AssemblyInfo.cs and have a "Themes" folder with a "Generic.xaml" to make it work). 

Using the code 

The program is very simple to use. Simply type some text into the search box and the number of items which are available to the listview for viewing will be limited so only those items which contain your search request will be visible. In addition you can click the "First Name" and "Last Name" columns to sort the items.

The HighlightTextBox control that was created for this project is also highly reusable for other applications so it's worthwhile to get acquainted with its simple design and use: 

To use this control, simply specify what you want to highlight with the HighlightPhrase property and if this string fragment is contained in the Text property then it will be highlighted using the HighlightBrush

This control was used in the DataTemplate's of the listview to define how to handle the rendering of listview items dynamically. Here is a minimal representation of the actual XAML: 

<ListView Name="listView"


            <GridViewColumn Width="200">
                <GridViewColumnHeader Content="First Name" />
                        <controls:HighlightTextBlock Text="{Binding Path=FirstName}" 

                           HighlightPhrase="{Binding ElementName=textBoxSearch, Path=Text}" />
            <GridViewColumn Width="200">
                <GridViewColumnHeader Content="Last Name" />
                        <controls:HighlightTextBlock Text="{Binding Path=LastName}" 

                           HighlightPhrase="{Binding ElementName=textBoxSearch, Path=Text}" />

Points of Interest

One thing I learned while writing this code is potentially the solution to this problem: WPF ListBox VirtualizingStackPanel.VirtualizationMode=“Recycling” causes same list items to always appear when scrolling.

Now I didn't encounter the exact same issue, but I did encounter similar behavior, i.e. setting the virtualization mode to standard stops the problem (but hurts performance) and it only happens when scrolling. In my version of the problem the highlighting wasn't showing up when you rapidly scrolled through items, but for the items that you could see when you first typed in the search box it worked perfectly. 

The solution was overloading the "Text" and "TextProperty" of HighlightTextBox and setting a PropertyChangedCallback which caused the highlighting to be re-applied when the text is changed (i know, this seems like a nobrainer now because the lack of this code is definitely a bug because you need to update the highlighting if the text changes just as much as you need to update it if the highlight phrase changes, but it's amazing how far I got with what seemed like otherwise perfect code to only see the problem when scrolling in recycling-virtualization mode).

The reason the new "Text" property fixes the problem is because when you scroll, while in recycling-virtualization mode, the existing instances of HighlightTextBox's are getting their Text property set, and perhaps i was expecting the HighlightPhrase property to also get set but that only actually happens (as per my binding in MainWindow.xaml) when you type in that search textbox.  

So to summarize: If you have any issues in recycling mode, it's the control you use in your DataTemplate that needs fixing. 


  • June 25th 2010 : Fixed bug. Also cleaned up the code + added a way to see the virtualization in action, i.e. more detailed information in the window title.
  • June 24th 2012 : Initial post.  


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Bravo Hex6-Feb-17 17:04
memberBravo Hex6-Feb-17 17:04 
PraiseMessage Closed Pin
18-Oct-16 21:02
memberer.vks18-Oct-16 21:02 
QuestionGreat post! Pin
d347hm4n26-Nov-14 2:30
memberd347hm4n26-Nov-14 2:30 
GeneralMy vote of 4 Pin
Octopus11262-Jul-13 10:08
memberOctopus11262-Jul-13 10:08 
BugHighlight has always the default color Pin
capibara25-Jun-12 3:25
membercapibara25-Jun-12 3:25 
GeneralRe: Highlight has always the default color Pin
FocusedWolf25-Jun-12 9:42
memberFocusedWolf25-Jun-12 9:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180621.3 | Last Updated 25 Jun 2012
Article Copyright 2012 by FocusedWolf
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid