ICollectionView.Refresh 不会重新过滤集合

发布于 2024-12-14 22:36:52 字数 297 浏览 3 评论 0原文

我的视图模型中有 3 个级联的 ICollectionView,其中一个依赖于另一个。 第一个不绑定到视图上的控件,而是用作即将作为主从细节向用户显示的两个即将出现的控件的关键过滤器。

我的问题是,我将一个过滤器谓词附加到主集合视图,但是当我从中调用 Refresh 时,它根本不会进入过滤器。我什至尝试从过滤器谓词中抛出异常,这样万一调试器无法到达该代码,我仍然会看到它到达,但没有抛出异常。

我调用 Refresh 但它没有将我带到过滤谓词 - 这意味着它没有重新创建视图,这可能是什么原因?

I have 3 cascading ICollectionViews in my view model where one relies on the other.
The first one, isn't bound to a control on the view but is rather used as a key-filter for the two upcoming ones that are displayed to user as master-detail.

My problem is, I attached a filter predicate to the main collection-view, but when I call Refresh from it, it doesn't go to the filter at all. I even tried throwing an exception from the filter predicate so in case the debugger cannot reach that code I will still see it arrived, but no exception was thrown.

What can be the reason that I call Refresh and it doesn't take me to the filter predicate - meaning it's not recreating the view?

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

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

发布评论

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

评论(1

蓝海似她心 2024-12-21 22:36:52

我找到了一个可行的解决方法,但我不喜欢它。

我正在做的是再次重置 Filter 属性,这样就可以完成任务。
我在 Reflector 中徘徊了一会儿,试图找到我错过的东西,但没有成功地澄清其中的怪癖。

private ICollectionView _Products;
public ICollectionView Products
{
  get
  {
    if (_Products == null)
    {
      _Products = 
         CollectionViewSource.GetDefaultView(ProductsLibrary.SupportedProducts);
      _Products.Filter = product => FilterProduct((Product)product);
    }

    return _Products;
  }
}
private bool FilterProduct(Product product)
{
  /**********************/
}

////////////////////

private void Search()
{
  //Products.Refresh();
  Products.Filter = product => FilterProduct((Product)product);
  Categories.Refresh();
  CategoryProducts.Refresh();
}

更新

我找到了一个更简单的解决方案。

我将第一个 ICollectionView 更改为简单的 IEnumerable,返回 Linq 查询。

不知道刷新不起作用的原因,但是在其他集合视图(绑定到 UI)上,刷新确实起作用,因此 IEnumerable 的 linq 过滤器确实起作用工作。

I found a workaround that works, but I don't like it.

What I'm doing is resetting the Filter property again, that does the job.
I wandered around a bit in Reflector trying to find what am I missing but didn't have great success clarifying what's the quirk.

private ICollectionView _Products;
public ICollectionView Products
{
  get
  {
    if (_Products == null)
    {
      _Products = 
         CollectionViewSource.GetDefaultView(ProductsLibrary.SupportedProducts);
      _Products.Filter = product => FilterProduct((Product)product);
    }

    return _Products;
  }
}
private bool FilterProduct(Product product)
{
  /**********************/
}

////////////////////

private void Search()
{
  //Products.Refresh();
  Products.Filter = product => FilterProduct((Product)product);
  Categories.Refresh();
  CategoryProducts.Refresh();
}

UPDATE

I've found a much simpler solution.

I changed the first ICollectionView to a simple IEnumerable<Product>, returning a Linq query.

Without finding out why the refresh didn't work, however on the other collection views (that are bound to the UI) the refresh does work, and so, the linq filter of the IEnumerable<Product> does the job.

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