筛选后的CollectionView计数错误 [英] Filtered CollectionView Gives Wrong Count

查看:83
本文介绍了筛选后的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计数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆