filter不是icollectionview中的事件

时间:2014-06-13 01:01:16

标签: c# wpf vb.net

我正在基于代码here

在WPF中构建一个filteredComboBox

我已将代码转换为VB.Net,因为这正是项目所使用的。正在使用的代码调用了在属性上添加处理程序,但它不起作用。很长一段时间我没有做太多这种样式代码而且有点迷失。有什么想法可以在这里解决这个问题吗?

这是C#的原始部分

protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            if (newValue != null)
            {
                ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
                view.Filter += this.FilterPredicate;
            }

            if (oldValue != null)
            {
                ICollectionView view = CollectionViewSource.GetDefaultView(oldValue);
                view.Filter -= this.FilterPredicate;
            }

            base.OnItemsSourceChanged(oldValue, newValue);
        }

这是转换后的VB

<summary>
 Keep the filter if the ItemsSource is explicitly changed.
 </summary>
 <param name="oldValue">The previous value of the filter.</param>
 <param name="newValue">The current value of the filter.</param>

Protected Overrides Sub OnItemsSourceChanged(oldValue As IEnumerable, newValue As IEnumerable)
    If newValue IsNot Nothing Then
        'Dim view As ICollectionView = CollectionViewSource.GetDefaultView(newValue)
        ' AddHandler view.Filter, AddressOf Me.FilterPredicate

        AddHandler CollectionViewSource.GetDefaultView(newValue).Filter, AddressOf Me.FilterPredicate
    End If

    If oldValue IsNot Nothing Then
        Dim view As ICollectionView = CollectionViewSource.GetDefaultView(oldValue)
        RemoveHandler view.Filter, AddressOf Me.FilterPredicate
    End If

    MyBase.OnItemsSourceChanged(oldValue, newValue)
End Sub

错误是&#34;过滤器不是System.ComponentModel.ICollectionView&#39;的事件。

1 个答案:

答案 0 :(得分:1)

由于filter是ICollectionView中的属性,因此您可能无法附加原始CollectionView之类的事件。所以你可以直接指向谓词方法

例如。通过linq

view.Filter = Function(item) CType(item, YourClass).Check

您案例的样本

Protected Overrides Sub OnItemsSourceChanged(oldValue As IEnumerable, newValue As IEnumerable)
    If newValue IsNot Nothing Then
        Dim view As ICollectionView = CollectionViewSource.GetDefaultView(newValue)

        'assign predicate method
        view.Filter= AddressOf Me.FilterPredicate
    End If

    If oldValue IsNot Nothing Then
        Dim view As ICollectionView = CollectionViewSource.GetDefaultView(oldValue)

        'unassign predicate
        view.Filter = Nothing
    End If

    MyBase.OnItemsSourceChanged(oldValue, newValue)
End Sub