Click here to Skip to main content
13,509,654 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


36 bookmarked
Posted 20 Aug 2010

Highlight Searched Text in WPF ListView

, 20 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Solution for highlighting searched text in WPF ListView


I wrote this as an inspiration for how to search in Microsoft Outlook. Imagine that you have a ListView in a WPF application and you are looking for a text in the individual items. It would be useful to highlight the text. The code used in this demo can be adjusted wherever it's appropriate and the highlighted text is displayed by using the TextBox control.


It is assumed you have basic knowledge of C# and WPF. This application is used instead of Text property in TextBlock control, Inline property and class Run through which we can change the text properties (such as Foreground, Background, FontSize, etc.) .

Using the Code

Create a Listview and TextBlock search. Add a TextBox TextChanged event for an instant search in ListView.

<DockPanel LastChildFill="True">      
  <StackPanel  HorizontalAlignment="Left" DockPanel.Dock="Top">           
              <Label Name="labelSearch" Content="Search"/>  
              <TextBox Name="textboxsearch" Width="309" 


       <ListView Name="listview" Width="310" DockPanel.Dock="Top" 

	HorizontalAlignment="Left" FontWeight="Bold" FontSize="16" AlternationCount="2"

       	ItemContainerStyle="{StaticResource AlternateItemStyle}">
           <GridViewColumn Header="First Name" 

		DisplayMemberBinding="{Binding Path=FirstName}">
           <GridViewColumn Header="Last Name" 

		DisplayMemberBinding="{Binding Path=LastName}">


Use TextChanged event to find all the items in ListViewItem with the recursive method FindListViewItem. GetChildrenCount with VisualTreeHelper class will bring the number children object.

 private void textboxsearch_TextChanged(object sender, TextChangedEventArgs e)
        public void FindListViewItem(DependencyObject obj)
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                ListViewItem lv = obj as ListViewItem;
                if (lv != null)
                FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));

And if DependecyObject type is ListViewItem, use a recursive method HighlightText which looks in ListViewItem for TextBlock objects.

private void HighlightText(Object itx)
            if (itx != null)
                if (itx is TextBlock)

If the object is TextBlock type, create a new instance RegEx.

(Parameter RegExOptions.IgnoreCase ignores uppercase and lowercase letters.)

Inserting brackets, you will reach the division of text into the field, which will include the searched text.

regex = new Regex("(" + textboxsearch.Text + ")", RegexOptions.IgnoreCase);

The text in the TextBlock divides it into a string array of assistance RegEx.Split. Clear the original text with Inlines.Clear.


And again in the foreach cycle, create the text that compares the expression of the field with the search terms with RegEx.Match.

foreach (var item in substrings)  
if (regex.Match(item).Success)

If identical, then use the Run class, create new Inline element, add text, change the background color, and add text to TextBlock with Inlines.Add methods. Otherwise, just add text.

Run runx = new Run(item);

runx.Background = Brushes.Red;


The code to change text in the background in full:

foreach (var item in substrings)
                        if (regex.Match(item).Success)
                            Run runx = new Run(item);
                            runx.Background = Brushes.Red;

Now we can start the project and try searching.


This application does not create any special effects when you highlight the text, but I think it can serve as an inspiration.

I hope you found it useful and thanks for reading.


  • 20th August, 2010: Initial post


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


About the Author

Jan Vilášek
Software Developer student
Czech Republic Czech Republic
No Biography provided

You may also be interested in...

Comments and Discussions

PraiseMessage Closed Pin
18-Oct-16 21:01
memberer.vks18-Oct-16 21:01 
QuestionOptimizations Pin
FocusedWolf22-Jun-12 6:55
memberFocusedWolf22-Jun-12 6:55 
AnswerA full rewrite ;P Pin
FocusedWolf22-Jun-12 11:03
memberFocusedWolf22-Jun-12 11:03 
Questionworks well for small documents, but how can i speed it up? Pin
I_Need_Help20-Dec-11 6:31
memberI_Need_Help20-Dec-11 6:31 
GeneralListview Contents truncated and reordered Pin
muthuprakash25-Apr-11 0:53
membermuthuprakash25-Apr-11 0:53 
GeneralRe: Listview Contents truncated and reordered Pin
Jan Vilášek7-May-11 4:23
memberJan Vilášek7-May-11 4:23 
GeneralThank You Pin
muthuprakash21-Apr-11 23:25
membermuthuprakash21-Apr-11 23:25 
GeneralRe: Thank You [modified] Pin
Jan Vilášek22-Apr-11 5:54
memberJan Vilášek22-Apr-11 5:54 
GeneralRe: Thank You Pin
muthuprakash22-Apr-11 21:01
membermuthuprakash22-Apr-11 21:01 
GeneralListView Not highlighted fully Pin
muthuprakash21-Apr-11 23:24
membermuthuprakash21-Apr-11 23:24 
GeneralRe: ListView Not highlighted fully Pin
Member 834570123-Mar-12 8:47
memberMember 834570123-Mar-12 8:47 
GeneralVery good code! Pin
Member 155142626-Nov-10 10:43
memberMember 155142626-Nov-10 10:43 
GeneralRe: Very good code! Pin
Jan Vilášek27-Nov-10 0:26
memberJan Vilášek27-Nov-10 0:26 
GeneralRe: Very good code! Pin
Member 155142628-Nov-10 20:43
memberMember 155142628-Nov-10 20:43 
GeneralRe: Very good code! Pin
Jan Vilášek29-Nov-10 4:38
memberJan Vilášek29-Nov-10 4:38 
GeneralCool!! Pin
DnGutierrez21-Sep-10 11:06
memberDnGutierrez21-Sep-10 11:06 
GeneralMy vote of 5 Pin
DnGutierrez21-Sep-10 11:03
memberDnGutierrez21-Sep-10 11:03 
GeneralGreat sample [modified] Pin
zameb1-Sep-10 22:56
memberzameb1-Sep-10 22:56 
Generalmost cool Pin
christoph braendle20-Aug-10 9:24
memberchristoph braendle20-Aug-10 9:24 

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 | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 20 Aug 2010
Article Copyright 2010 by Jan Vilášek
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid