Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: ICollectionView - Filter one list if item is contained in another list?
    primarykey
    data
    text
    <p>Basically, I have 2 ListView's each binding to a different <code>ItemsSource</code>.</p> <p>List 1 <b>can not</b> be changed (it's a <code>ReadOnlyObservableCollection</code>).<br /> List 2 <b>can</b> be changed (through user interaction).<br /></p> <p>I need to add a filter to List 1 so that it doesn't display anything that's found in List 2. This is my code so far...</p> <pre><code>view = CollectionViewSource.GetDefaultView(List1.ItemsSource); view.Filter += o =&gt; { MyItem item = o as MyItem; return List2.ItemsSource.??; }; </code></pre> <p><hr> List2.ItemsSource comes back as an IEnumerable instead of ObservableCollection (what it really is). I want to do this as efficiently as possible, so I wasn't sure if I should:</p> <ol> <li>Explicitly Cast the ItemsSource as a IList to gain access to Contains?</li> <li>Iterate through the the ItemsSource myself to see if it contains the item?</li> <li>Use LINQ's Cast extension method to cast to an IList (or some other type) to gain access to Contains?</li> <li>Any other way?</li> </ol> <hr> <p><b>UPDATE:</b></p> <p>It doesn't seem to continue filtering items after the first time it renders:</p> <pre><code>view = CollectionViewSource.GetDefaultView(List1.ItemsSource); view.Filter += o =&gt; { MyItem item = (MyItem)o; var collection = (ObservableCollection&lt;MyItem&gt;)List2.ItemsSource; //return collection.Contains(item);//Filters out ALL items from the list return !collection.Contains(item); //Shows all items, but as I add items //to list 2 it doesn't filter items out of //list 1. }; </code></pre> <p><b>UPDATE 2:</b></p> <p>I think I understand why it's not reapplying the filter. The original collection is not raising a CollectionChanged notification, so it doesn't bother to run the filter again. Perhaps solving this part is better suited as a different question? But, in case anyone wants to answer it here:</p> <p>How can I get my List1 to reapply the filter when the List2 collection changes?</p> <p><b>UPDATE 3:</b> I asked how to tie into the collectionchanged event in a <a href="https://stackoverflow.com/questions/5806172/wpf-how-do-i-hook-into-a-listviews-collectionchanged-notification">separate SO question</a> and got my answer.</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.
 

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