Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:34:57+00:00 2026-06-15T10:34:57+00:00

I have a listview and a search textbox. When I type any character or

  • 0

I have a listview and a search textbox. When I type any character or symbol into the textbox, then it matches the typed text with the listbox items. If there is any match then it highlights the matched item. In that point, I want to remove the not matched items from the listview. Only the matched items will remain in the listview. What do you recommend me to solve that ? My other code is below.

 private void TxtSearch_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
 {
        ListControl lc = getactivListview();
        FindListViewItem(lc);
 }


 private void FindListViewItem(DependencyObject obj)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            ListViewItem lv = obj as ListViewItem;
            if (lv != null)
            {
                HighlightText(lv);
            }
            FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));
        }
    }

    private void HighlightText(Object itx)
    {
        if (itx != null)
        {
            if (itx is TextBlock)
            {
                Regex regex = new Regex("(" +TxtSearch.Text + ")", RegexOptions.IgnoreCase);
                TextBlock tb = itx as TextBlock;
                if (TxtSearch.Text.Length == 0)
                {
                    string str = tb.Text;
                    tb.Inlines.Clear();
                    tb.Inlines.Add(str);
                    return;
                }
                string[] substrings = regex.Split(tb.Text);
                tb.Inlines.Clear();
                foreach (var item in substrings)
                {
                    if (regex.Match(item).Success)
                    {
                        Run runx = new Run(item);
                        runx.Background = Brushes.Lime;
                        tb.Inlines.Add(runx);

                        if (tb.IsMouseOver)
                        {
                            tb.IsEnabled = false;
                        }
                    }
                    else
                    {
                        tb.Inlines.Add(item);
                        tb.IsEnabled = false;
                    }
                }

                return;
            }
            else
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                {
                    HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:34:58+00:00Added an answer on June 15, 2026 at 10:34 am

    Well, the easiest and most natural way in WPF is use of databinding and filter ModelView collection on it.

    for example:

    public class ModelView 
    {
        private IEnumerable<Data> _data = ....
        public string Filtrer {get;set;}
    
        public IEnumerable<Data> GetData() {
    
              return FilteredData();
        }
    
        private IEnumerable<Data> FilteredData()
       {
          if(string.IsNullOrEmpty(Filter))
             return _data;
    
          return   _data.Where(x=>x...Filter); //SOME CONDITON EXECUTION
       }
    }
    

    You bind to view GetData() which return either full or filtered collection, based on the absence or presence of the filter string.

    Like an example how can be databinding implemented on ListView (it’s not a shot topic and hardly can be explained in this simple answer), can have a look on: Data Binding in WPF ListView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 controls in a WPF window.. a textbox, listbox and listview. The
I Have a ListView with many Items that is to be loaded in search.
I have a listview which has its datasource changed after update of a search
I have listview control.There is an option to remove selected items.After the user removes
I have a listview, which is used as an index to a simple search
I'm using ListView to show search results. I have a Coustomized arrayListAdapter which returns
I have a ListView that shows a list of search results. To make things
I have a simple list with a search. <ul data-role=listview data-inset=false data-filter=true data-iconpos=right> My
I have a custom control in winform, which is a search textbox. It has
I have a ListView and a EditText. I want to be search the name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.