Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Filter Property

CollectionView.Filter Property

Gets or sets a callback used to determine if an item is suitable for inclusion in the view.

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

Public Overridable Property Filter As Predicate(Of Object)
Dim instance As CollectionView
Dim value As Predicate(Of Object)

value = instance.Filter

instance.Filter = value
/** @property */
public Predicate<Object> get_Filter ()

/** @property */
public void set_Filter (Predicate<Object> value)

public function get Filter () : Predicate<Object>

public function set Filter (value : Predicate<Object>)

You cannot use this property in XAML.

Property Value

A method used to determine if an item is suitable for inclusion in the view.

Exception typeCondition


If the current implementation does not support filtering.

Simpler implementations do not support filtering and will throw a NotSupportedException. Use the CanFilter property to test if filtering is supported before assigning this property with a non-null value.

This example shows how to filter data in a view.

To create a filter, define a method that provides the filtering logic. The method is used as a callback and accepts a parameter of type object. The following method returns all the Order objects with the filled property set to "No", filtering out the rest of the objects.

Public Function Contains(ByVal de As Object) As Boolean
    Dim order1 As Order = TryCast(de, Order)
    Return (order1.Filled Is "No")
End Function

You can then apply the filter, as shown in the following example. In this example, myCollectionView is a ListCollectionView object.

myCollectionView.Filter = new Predicate<object>(Contains);

To undo filtering, you can set the Filter property to a null reference (Nothing in Visual Basic):

myCollectionView.Filter = null;

For information about how to create or obtain a view, see How to: Create a View of a Data Collection. For the complete example, see Sorting and Filtering Items in a View Sample.

If your view object comes from a CollectionViewSource object, you apply filtering logic by setting an event handler for the Filter event. In the following example, listingDataView is an instance of CollectionViewSource.

listingDataView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);

The following shows the implementation of the example ShowOnlyBargainsFilter filter event handler. This event handler uses the Accepted property to filter out AuctionItem objects that have a CurrentPrice of $25 or greater.

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
    AuctionItem product = e.Item as AuctionItem;
    if (product != null)
        // Filter out products with price 25 or above
        if (product.CurrentPrice < 25)
            e.Accepted = true;
            e.Accepted = false;

For the complete example, see Data Binding Demo.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

© 2015 Microsoft