过滤后的 CollectionView 给出错误的计数
根据文档,Count过滤后的 CollectionView 的值应该只是通过过滤器的项目数。鉴于此代码:
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。
我尝试在 CollectionView 上调用 Refresh,只是为了看看这是否会纠正结果,但没有任何变化。文档有误吗? 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;
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
代替
Try
instead of
如果你切换到 ListCollectionView,那么它会按预期工作:
这似乎适用于 CollectionView,所以这肯定指向一个错误:
If you switch to ListCollectionView, then it works as expected:
This seems to work for CollectionView, so this definitely points to a bug:
似乎有一个错误,我检查了反射器,如果您尝试调用“刷新”,可能会为您提供正确的计数。根据文档,他们说您不需要调用刷新,因为设置过滤器会自动刷新它,但我认为这不会发生,因为他们还提到它们缓存了上次更改的计数值。
如果您在添加项目之前设置过滤器,效果会很完美。否则您将必须调用“刷新”。
There seems a bug, I checked reflector may be if you try calling "Refresh" that should give you correct Count. As per documentation, they say that you do not need to call Refresh as setting filter will refresh it automatically but I think it is not happening as they also mention that they cache the value of count from last change.
It would work perfect if you set the Filter before you add items. Or you will have to call Refresh.