Search code examples
wpfdatatriggercollectionviewsource

WPF CollectionViewSource.Filter Performance v. DataTrigger


The Entity Framework provides Trackable Entities who have a property called ChangeTracker.State. Using this we can identify if an Entity is Deleted.

In our list we do NOT want to show Deleted entities.

Which is faster?

Method 1: Using a CollectionViewSource.Filter to Test and remove the Record

<CollectionViewSource Filter="ViewSource_Filter" />

private void ViewSource_Filter(object sender, FilterEventArgs e)
{
    var _Item = e.Item as ITrackableEntity;
    e.Accepted = _Item.ChangeTracker.State != ObjectState.Deleted;
}

Method 2: Adding a DataTrigger to the ItemTemplate.DataTemplate to Test and Hide the Item

<DataTemplate.Resources>
    <Style TargetType="{x:Type DockPanel}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ChangeTracker.State}">
                <DataTrigger.Value>
                    <entities:ObjectState>Deleted</entities:ObjectState>
                </DataTrigger.Value>
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataTemplate.Resources>

Thanks.


Solution

  • Method 1 is the correct answer.