筛选后的CollectionView计数错误

编程入门 行业动态 更新时间:2024-10-10 07:31:47
本文介绍了筛选后的CollectionView计数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据文档 ,经过过滤的CollectionView的计数应仅是通过过滤器的项目数。给出以下代码:

According to the documentation, the Count of a filtered CollectionView should only be the count of items that pass the filter. Given this code:

List<string> testList = new List<string>(); testList.Add("One"); testList.Add("Two"); testList.Add("Three"); testList.Add("1-One"); testList.Add("1-Two"); testList.Add("1-Three"); CollectionView testView = new CollectionView(testList); int testCount1 = testView.Count; testView.Filter = (i) => i.ToString().StartsWith("1-"); int testCount2 = testView.Count;

因此,我希望testCount1为6,而testCount2为3。但是,两者均为6。如果我手动遍历CollectionView并计数项目,则得到3,但Count始终返回6。

I would therefore expect testCount1 to be 6, and testCount2 to be 3. However, both are 6. If I manually iterate through the CollectionView and count the items, I do get 3, but Count returns 6 always.

我尝试在CollectionView上调用Refresh,只是为了看看是否可以纠正结果,但是没有变化。文档是否有误? CollectionView中有错误吗?我是在做我看不见的错吗?

I've tried calling Refresh on the CollectionView, just to see if that would correct the result, but there was no change. Is the documentation wrong? Is there a bug in CollectionView? Am I doing something wrong that I just can't see?

推荐答案

尝试

ICollectionView _cvs = CollectionViewSource.GetDefaultView(testList);

而不是

CollectionView testView = new CollectionView(testList);

更多推荐

筛选后的CollectionView计数错误

本文发布于:2023-10-30 18:28:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   CollectionView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!