Поделиться через


PagedCollectionView.Filter Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a callback that is used to determine whether an item is suited for inclusion in the view.

Namespace:  System.Windows.Data
Assembly:  System.Windows.Data (in System.Windows.Data.dll)

Syntax

'Declaration
Public Property Filter As Predicate(Of Object)
public Predicate<Object> Filter { get; set; }

Property Value

Type: System.Predicate<Object>
A method that is used to determine whether an item is suited for inclusion in the view.

Implements

ICollectionView.Filter

Exceptions

Exception Condition
NotSupportedException

The implementation does not support filtering.

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

InvalidOperationException

IsAddingNew is true.

-or-

IsEditingItem is true.

Remarks

The Filter is a callback set by the consumer of the ICollectionView and used by the implementation of the ICollectionView to determine whether an item is suited for inclusion in the view.

Examples

The following code example demonstrates how to use the Filter property to remove completed tasks from a DataGrid display. The filter is applied when a CheckBox is Checked, and removed when the CheckBox is Unchecked. This example is part of a larger example available in the How to: Group, Sort, and Filter Data in the DataGrid Control topic.

Private Sub CheckBox_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim pcv As PagedCollectionView = Me.dataGrid1.ItemsSource
    If pcv IsNot Nothing & pcv.CanFilter = True Then
        ' Apply the filter.
        pcv.Filter = New Predicate(Of Object)(AddressOf FilterCompletedTasks)
    End If
End Sub

Private Sub CheckBox_Unchecked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim pcv As PagedCollectionView = Me.dataGrid1.ItemsSource
    If pcv IsNot Nothing Then
        ' Remove the filter.
        pcv.Filter = Nothing
    End If
End Sub

Public Function FilterCompletedTasks(ByVal t As Object) As Boolean
    Dim _task As New Task
    _task = t
    Return _task.Complete = False
End Function
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    PagedCollectionView pcv = this.dataGrid1.ItemsSource as PagedCollectionView;
    if (pcv != null && pcv.CanFilter == true)
    {
        // Apply the filter.
        pcv.Filter = new Predicate<object>(FilterCompletedTasks);
    }
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    PagedCollectionView pcv = this.dataGrid1.ItemsSource as PagedCollectionView;
    if (pcv != null)
    {
        // Remove the filter.
        pcv.Filter = null;
    }
}

public bool FilterCompletedTasks(object t)
{
    Task task = t as Task;
    return (task.Complete == false);
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.