Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight 4 CollectionViewSource ListBox Filters Based on DataType
    text
    copied!<p>I have a ListBox which I am loading with objects of three types which all inherit from the same parent type. I would like to filter out the objects in the ListBox based on their type using a CollectionViewSource but I am having trouble writing the filter(s).</p> <p>My XAML is as follows: </p> <pre><code>&lt;UserControl.Resources&gt; &lt;CollectionViewSource x:Key="eventsViewSource" Source="{Binding lifeCycleEvents}" &gt; &lt;CollectionViewSource.SortDescriptions&gt; &lt;compMod:SortDescription PropertyName="Date" Direction="Ascending"/&gt; &lt;/CollectionViewSource.SortDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;/UserControl.Resources&gt; &lt;ListBox Name="lstEventHistory" ItemsSource="{Binding Source={StaticResource eventsViewSource}}"&gt; ... &lt;/ListBox&gt; </code></pre> <p>In the code-behind I'm loading an observable collection of the objects returned from an async method call: </p> <pre><code>private ObservableCollection&lt;LifeCycleEvent&gt; lifeCycleEvents; ... lifeCycleEvents= e.Result; CollectionViewSource eventsViewSource = this.Resources["eventsViewSource"] as CollectionViewSource; eventsViewSource.Source = lifeCycleEvents; </code></pre> <p>Now I would like to write a filter that will only show objects of a certain type based on checkboxes the user will click in the UI.</p> <p>The following is a failed attempt at writing one filter: </p> <pre><code>eventsViewSource.Filter = new Predicate&lt;object&gt;(rmaFilter); public bool rmaFilter(object item) { if(item.GetType() == typeof(RmaEvent)) return true; else return false; } </code></pre> <p>This gives the following error: "The event System.Windows.Data.CollectionViewSrouce.Filter can only appear on the left hand side of += or -=".</p> <p>I haven't been able to find any tutorials that do exactly what I need. Those that do, depend on a default view which doesn't exist in Silverlight. One such tutorial I looked at can be found <a href="http://www.zagstudio.com/blog/454#.UWXSyZPvtUg" rel="nofollow">here</a></p> <p>Any tips are appreciated!</p>
 

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