Search code examples
wpffiltercollectionviewsource

How to use multiple filters on CollectionView in VB.NET


I'm trying to use multiple filters on a CollectionView - using this approach: http://bea.stollnitz.com/blog/?p=32

I have worked out the syntax for adding the filters to my CollectionView

I now want to know how I can add additional parameters to my Filter method.

e.g.

Public Shared Sub FilterByAge(ByVal Item As Object, ByVal e As FilterEventArgs, ByVal Age As Int32)
    '
    '
    Dim PersonToFilter As Person = TryCast(e.Item, Person)
    '
    '
    If Not PersonToFilter.Age = Age Then
        '
        e.Accepted = False
        '
    End If
    '
    '
End Sub

Would it be possible to do this using a Deletegate? I'm not very familiar with them though:

Can anyone point me in the right direction for adding multiple CollectionViewSource filters in VB.NET with additional filter paramters?

Thanks Ben


Solution

  • I now want to know how I can add additional parameters to my Filter method.

    You can't do that, the handler signature must match the event signature. The easiest solution is to store the age in a field and use that field in your handler:

    Private _age As Int32
    
    Public Sub FilterByAge(ByVal Item As Object, ByVal e As FilterEventArgs)
        '
        '
        Dim PersonToFilter As Person = TryCast(e.Item, Person)
        '
        '
        If Not PersonToFilter.Age = _age Then
            '
            e.Accepted = False
            '
        End If
        '
        '
    End Sub