Articles in this section

How to search and highlight items in WPF TreeViewAdv?

In WPF TreeViewAdv to search and highlight the item, Search method has been implemented which is used to compare the search text with model items and highlighted the matched text in TextBlock inside the TreeViewItemAdv.

if (mainitem != null && MatchSearchText(model.Header))
{
    // Function which is used to apply inline to the TextBlock
    ApplyInline(mainitem, false);
}

Options like Search, FindPrevious, Find Next, SearchType (Contains, EndsWith, StartsWith options) has been used to match search text using ApplyInline method and apply the SearchBrush for highlighting the search text.

if (!string.IsNullOrEmpty(tempSearchText))
{
    if (this.SearchType == SearchType.StartsWith)
    {
        if (!(bool)CaseSensitiveSearchCheckBox.IsChecked)
            regex = new Regex("^(" + tempSearchText + ")", RegexOptions.IgnoreCase);
        else
            regex = new Regex("^(" + tempSearchText + ")", RegexOptions.None);
    }
    else if (this.SearchType == SearchType.EndsWith)
    {
        if (!(bool)CaseSensitiveSearchCheckBox.IsChecked)
            regex = new Regex("(" + tempSearchText + ")$", RegexOptions.IgnoreCase);
        else
            regex = new Regex("(" + tempSearchText + ")$", RegexOptions.None);
    }
    else
    {
        if (!(bool)CaseSensitiveSearchCheckBox.IsChecked)
            regex = new Regex("(" + tempSearchText + ")", RegexOptions.IgnoreCase);
        else
            regex = new Regex("(" + tempSearchText + ")", RegexOptions.None);
    }
}

The following screenshot illustrates how to highlight and perform search in TreeViewAdv.

Highlight and perform in WPF TreeView

 

 View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied