谓语<对象>CollectionView.过滤器在不同的类中

本文关键字:过滤器 对象 CollectionView 谓语 | 更新日期: 2023-09-27 17:54:11

我有一个TagTypeController类,它为WPF UserControl的控制器提供了一个集合视图,它保存了一个对集合视图的私有引用。

_ttController = new TagTypeController(_isProd);
CollectionView tagTypeList = _ttController.getTagTypes();

在TagTypeController中,当创建CollectionView时,我正在设置过滤器委托

if (_tagTypeList == null)
    _tagTypeList = new CollectionView(CollectionViewSource.GetDefaultView(_tagTypeTable));
    _tagTypeList.Filter = new Predicate<object>(filterTagTypes);

我想在TagTypeController类中找到该集合视图的过滤等所有逻辑。问题是,当UserControl的TextBox中的文本发生变化时,我通过委托给UserControl的控制器来响应该事件。当我要求tagTypeList刷新时,它不调用filterTagTypes方法。是否不可能将过滤器委托放在不同的类中?

谢谢。编辑:添加请求的代码

//parse the string to get just the portion after the last comma and space
Int32 _lastComma = _tempText.LastIndexOf(",");
_ttController.searchText = _tempText.Substring(_lastComma + 1).Trim();
tagTypeList.Refresh();

谓语<对象>CollectionView.过滤器在不同的类中

我认为问题可能是您正在使用过滤器谓词而不是事件。如果你看一下CollectionView文档它说:

如果你的视图对象来自CollectionViewSource对象,应用通过设置事件过滤逻辑过滤器事件的处理程序。

不设置属性而是使用事件处理程序代码看起来像

_tagTypeList.Filter += FilterTagTypesHandler;

其中FilterTagTypesHandler定义为

private void FilterTagTypesHandler(object sender, FilterEventArgs e){
  //do filtering
}

另一种可能性是您正在创建一个新的CollectionView而不是强制转换GetDefaultView()的结果。当你这样做时,你可能会失去与控件的连接。如果你看一下CollectionViewSource的文档它的推荐使用方式是

myCollectionView = (CollectionView)
    CollectionViewSource.GetDefaultView(rootElem.DataContext);