过滤后的 CollectionView 给出错误的计数

发布于 2024-11-01 01:53:54 字数 796 浏览 1 评论 0原文

根据文档,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 技术交流群。

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

发布评论

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

评论(3

森末i 2024-11-08 01:53:54

尝试

ICollectionView _cvs = CollectionViewSource.GetDefaultView(testList);

代替

CollectionView testView = new CollectionView(testList);    

Try

ICollectionView _cvs = CollectionViewSource.GetDefaultView(testList);

instead of

CollectionView testView = new CollectionView(testList);    
深海里的那抹蓝 2024-11-08 01:53:54

如果你切换到 ListCollectionView,那么它会按预期工作:

CollectionView testView = new ListCollectionView(testList);
int testCount1 = testView.Count;
testView.Filter = (i) => i.ToString().StartsWith("1-");
int testCount2 = testView.Count;

这似乎适用于 CollectionView,所以这肯定指向一个错误:

CollectionView testView = new CollectionView(this.GetTestStrings());

private IEnumerable<string> GetTestStrings() {
    yield return "One";
    yield return "Two";
    yield return "Three";
    yield return "1-One";
    yield return "1-Two";
    yield return "1-Three";
}

If you switch to ListCollectionView, then it works as expected:

CollectionView testView = new ListCollectionView(testList);
int testCount1 = testView.Count;
testView.Filter = (i) => i.ToString().StartsWith("1-");
int testCount2 = testView.Count;

This seems to work for CollectionView, so this definitely points to a bug:

CollectionView testView = new CollectionView(this.GetTestStrings());

private IEnumerable<string> GetTestStrings() {
    yield return "One";
    yield return "Two";
    yield return "Three";
    yield return "1-One";
    yield return "1-Two";
    yield return "1-Three";
}
巴黎盛开的樱花 2024-11-08 01:53:54

似乎有一个错误,我检查了反射器,如果您尝试调用“刷新”,可能会为您提供正确的计数。根据文档,他们说您不需要调用刷新,因为设置过滤器会自动刷新它,但我认为这不会发生,因为他们还提到它们缓存了上次更改的计数值。

如果您在添加项目之前设置过滤器,效果会很完美。否则您将必须调用“刷新”。

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.

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