views:

23

answers:

1

Hi. I'm wondering if I can do something like this with CollectionViewSource too. I have a DataTemplate that looks like this:

<DataTemplate DataType="{x:Type local:MyObject}">
    <StackPanel Orientation="Horizontal">
     <Grid>
      <Image Source="Images\gear16.png" />
      <Image Source="Images\disk.gif" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
          Visibility="{Binding MyProp, Converter={StaticResource BooleanToVisibilityConverter}}" />
     </Grid>
     <TextBlock Margin="5,0,0,0" Text="{Binding Name}" VerticalAlignment="Center" />
    </StackPanel>
</DataTemplate>

So of course, everything bound to that type of object takes that DataTemplate, or in other words, every object of type MyObject gets that datasource. Can I do something similar for CollectionViewSource? Make every object of type MyObject go through the filtering methods?

The problem is that I have several instances of this collection oF MyObject, and it will be very difficult to filter one by one (I think), and still handle updates to data and everything, so I'm wondering if there is a solution like this.

Thanks!

+1  A: 

You can use CollectionView.Filter property to perform filtering. There's no way for any "group" filtering, only "one by one" as you say. You can read here about filtering.
Your filtering handler will look like this:

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
    if (e.Item is MyObject)
    {
        e.Accepted = true;
    }
    else
    {
        e.Accepted = false;
    }
}

Hope it helps.

levanovd