过滤的CollectionView提供错误的计数

Dav*_*lin 10 wpf collectionview

根据文档,过滤的CollectionView的Count应该只是通过过滤器的项目数.鉴于此代码:

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;
Run Code Online (Sandbox Code Playgroud)

因此我希望testCount1为6,testCount2为3.但是,两者都是6.如果我手动迭代CollectionView并计算项目,我会得到3,但Count总是返回6.

我试过在CollectionView上调用Refresh,只是为了看看是否会纠正结果,但没有变化.文档错了吗?CollectionView中有错误吗?我做错了什么,我看不到?

Bru*_*uno 5

尝试

ICollectionView _cvs = CollectionViewSource.GetDefaultView(testList);
Run Code Online (Sandbox Code Playgroud)

代替

CollectionView testView = new CollectionView(testList);    
Run Code Online (Sandbox Code Playgroud)


Aka*_*ava 0

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

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