Filtered CollectionView Gives Wrong Count

青春壹個敷衍的年華 提交于 2019-12-05 02:55:51

Try

ICollectionView _cvs = CollectionViewSource.GetDefaultView(testList);

instead of

CollectionView testView = new CollectionView(testList);    

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";
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!