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 8764097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:56:44+00:00 2026-06-13T15:56:44+00:00

I have a ListView that I’m filling with an ObservableCollection I’m filtering the list

  • 0

I have a ListView that I’m filling with an ObservableCollection

I’m filtering the list from text entered in a TextBox

Here’s a section of the code I’m using:

    private void Filter_TextChanged(object sender, TextChangedEventArgs e)
    {
           view = CollectionViewSource.GetDefaultView(Elementos_Lista.ItemsSource);
           view.Filter = null;
           view.Filter = new Predicate<object>(FilterList);
    }

This works okay if I want to filter a list by just one criteria, but whenever I want to mix more than 1 textbox to do the filtering it always filters based on the ItemsSource, and not the current result set, meaning that there’s no accumulation of criteria.

Here’s my FilterList method.

 ItemDetail item = obj as ItemDetail;  
 if (item == null) return false;
 string  textFilter = txtFilter.Text; 
 if (textFilter.Trim().Length == 0) return true; //this returns the unfiltered results.
 if ((item.problema.ToLower().Contains(textFilter.ToLower()))) return true;  
 return false;  

Is there a way to filter the ObservableCollection (view) by multiple criteria, that aren’t always provided at the same time?.

I have tried to change the FilterList method to evaluate various textboxes, but I would have to make an IF statement to check all the possibilities of matching the criteria.

(filter1=value , filter2=value) OR 
(filter1=value , filter2=empty) OR 
(filter1=empty , filter2=value)

And since I’m planning to filter by at least 5 different controls, that wouldn’t be fun at all.

Example:

List:
Maria, Age:26
Jesus, Age:15
Angela, Age:15

First Filter

Filters:
Name: //Empty
Age: 15

Result:
Jesus, Age:15
Angela, Age:15

Second Filter:

Filters:
Name: Jesus
Age: 15

Result:
Jesus, Age:15

What I’m trying to do is to apply filters to the already filtered collection, and not the original one, and this approach overwrites the applied filter with the next one.

  • 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-13T15:56:45+00:00Added an answer on June 13, 2026 at 3:56 pm

    OK, let’s see, I had something similar lying around here…

    [ContractClass(typeof(CollectionViewFilterContracts<>))]
    public interface ICollectionViewFilter<in T> where T : class
    {
        bool FilterObject(T obj);
    }
    

    Contracts are optional of course (CodeContracts)

        [ContractClassFor(typeof(ICollectionViewFilter<>))]
        public abstract class CollectionViewFilterContracts<T> : ICollectionViewFilter<T> where T : class
        {
            public bool FilterObject(T obj)
            {
                Contract.Requires<ArgumentNullException>(obj != null, "Filtered object can't be null");
    
                return default(bool);
            }
        }
    

    And then the base implementation, you’re using only strings for comparison as far as I can tell, so here’s string-only version:

    public abstract class CollectionFilterBase<T> : ICollectionViewFilter<T> where T : class
        {
    
            private readonly Dictionary<string, string> filters = new Dictionary<string, string>(10);
            private readonly PropertyInfo[] properties;
    
            protected CollectionFilterBase()
            {
                 properties = typeof(T).GetProperties();
            }        
    
            protected void AddFilter(string memberName, string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    filters.Remove(memberName);
                    return;
                }
    
                filters[memberName] = value;
            }
    
            public virtual bool FilterObject(T objectToFilter)
            {
                 foreach (var filterValue in filters)
                 {
                     var property = properties.SingleOrDefault(x => x.Name == filterValue.Key);
                     if(property == null)
                         return false;
    
                     var propertyValue = property.GetValue(objectToFilter, null);
                     var stringValue = propertyValue == null ? null : propertyValue.ToString(); // or use string.Empty instead of null, depends what you're going to do with it.
                     // Now you have the property value and you have your 'filter' value in filterValue.Value, do the check, return false if it's not what you're looking for.
                     //The filter will run through all selected (non-empty) filters and if all of them check out, it will return true.
                 }
    
                return true;
            }
      }
    

    Now some meaningful implementation, let’s say this is your class, for simplicity:

    public class Person
    {
        public string Name {get;set;}
        public int Age {get;set;}
    }
    

    The filter implementation is – at the same time – the view model behind the view containing all your ‘filtering’ controls, so you bind the text box values to the properties accordingly.

    public class PersonFilter : CollectionFilterBase<Person>
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
    
            set
            {
                name = value;
                //NotifyPropertyChanged somehow, I'm using Caliburn.Micro most of the times, so:
                NotifyOfPropertyChange(() => Name);
                AddFilter("Name", Name);            
            }
        }
        private int age;
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
                //Same as above, notify
                AddFilter("Age", Age.ToString()) // only a string filter...
            }
        }
    }
    

    Then you have a PersonFilter object instance in your view model.

    ICollectionViewFilter<Person> personFilter = new PersonFilter();
    

    Then you just have to use the Filter event on the CollectionView to some method in the view model, e.g.:

    CollectionView.Filter += FilterPeople
    
    private void FilterPeople(object obj)
    {
        var person = obj as Person;
        if(person == null)
            return false;
        return personFilter.FilterObject(person);
    }
    

    Filter names have to be the same as the property names on the filtered object. 🙂

    And of course you’ll have to call the CollectionView.Refresh(); somewhere, you can move it to the filter (e.g. when the property changes, you can call CollectionView.Refresh() to see the changes immediately), you can call it in the event handler, however you want.

    It’s quite straightforward, although the performance could be better. Unless you’re filtering a metric ton of data with a few dozen filters, you shouldn’t have too many problems with adjusting and using the snippets. 🙂

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

Sidebar

Related Questions

I have a ListView that is bound to an ObservableCollection in the XAML code
I have a ListView that I'm populating with information from the media store. I
I have a listview that loads dynamic controls from xml/xslt <asp:ListView ID=DynamicFields runat=server DataSourceID=CustomFields
I have a listview that is populated by a by objects from an Offer
I have a ListView that shows a list of names. When you select a
I have a ListView that I'm populating with values from my database. If the
I have listview that binds from sqlite and groups by KEY_TITLE(field) so i need
I have a ListView that is filled by an ObservableCollection<MenuTrayItem> . In my resources
I have a ListView that is populated with rows. These rows come from an
I have a ListView that has a list of articles. Every X articles (10

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.