Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to filter ObservableCollection from various textbox (Multiple Criteria)?
    primarykey
    data
    text
    <p>I have a <code>ListView</code> that I'm filling with an <code>ObservableCollection</code> </p> <p>I'm filtering the list from text entered in a <code>TextBox</code></p> <p>Here's a section of the code I'm using:</p> <pre><code> private void Filter_TextChanged(object sender, TextChangedEventArgs e) { view = CollectionViewSource.GetDefaultView(Elementos_Lista.ItemsSource); view.Filter = null; view.Filter = new Predicate&lt;object&gt;(FilterList); } </code></pre> <p>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.</p> <p>Here's my FilterList method.</p> <pre><code> 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; </code></pre> <p>Is there a way to filter the ObservableCollection (view) by multiple criteria, that aren't always provided at the same time?.</p> <p>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.</p> <pre><code>(filter1=value , filter2=value) OR (filter1=value , filter2=empty) OR (filter1=empty , filter2=value) </code></pre> <p>And since I'm planning to filter by at least 5 different controls, that wouldn't be fun at all.</p> <p>Example:</p> <pre><code>List: Maria, Age:26 Jesus, Age:15 Angela, Age:15 </code></pre> <p>First Filter</p> <pre><code>Filters: Name: //Empty Age: 15 Result: Jesus, Age:15 Angela, Age:15 </code></pre> <p>Second Filter:</p> <pre><code>Filters: Name: Jesus Age: 15 Result: Jesus, Age:15 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload