Filter不是icollectionview中的事件
本文关键字:事件 icollectionview 不是 Filter | 更新日期: 2023-09-27 18:08:37
我正在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
错误是"Filter不是'System.ComponentModel.ICollectionView'的事件。
因为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