谓词可以吗? CollectionView.Filter 位于不同的类中吗?

发布于 2024-11-04 07:32:34 字数 951 浏览 1 评论 0原文

我有一个 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 类中找到该 collectionview 的所有过滤逻辑等。问题是,当 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();

I have a TagTypeController class that provides a collection view to a controller for a WPF UserControl, which holds a private reference to the collection view.

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

In the TagTypeController, when creating the CollectionView, I'm setting the filter delegate

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

I would like to locate all the logic for filtering, etc. of that collectionview in the TagTypeController class. The problem is, when the text changes in the TextBox of the UserControl, I'm responding to that event by delegating to the controller for the UserControl. When I ask the tagTypeList to refresh, it does not call the filterTagTypes method. Is it not possible to have the filter delegate in a different class?

Thanks.

EDIT: adding requested code

//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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2024-11-11 07:32:34

我认为问题可能是您使用的是过滤谓词而不是事件。如果您查看 CollectionView 文档它说:

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

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

_tagTypeList.Filter += FilterTagTypesHandler;

FilterTagTypesHandler 的定义类似

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

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

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

I think the problem might be that you are using the filter predicate rather than the event. If you look at the CollectionView documentation it says:

If your view object comes from a
CollectionViewSource object, you apply
filtering logic by setting an event
handler for the Filter event.

So instead of setting the property you want to use the event handler so the code would look like

_tagTypeList.Filter += FilterTagTypesHandler;

where FilterTagTypesHandler is defined like

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

The other possibility is that you are creating a new CollectionView instead of casting the result of GetDefaultView(). You are probably losing your connection to the control when you do that. If you look at the CollectionViewSource's documentation the reccommend way of using it is

myCollectionView = (CollectionView)
    CollectionViewSource.GetDefaultView(rootElem.DataContext);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文