How to get Items count from CollectionViewSource? How to get Items count from CollectionViewSource? wpf wpf

How to get Items count from CollectionViewSource?


You could also do _viewSource.View.Cast<object>().Count() for the filtered list and _viewSource.View.SourceCollection.Cast<object>().Count() for the original.


I think the better solution is, as usual, Linq!

_viewSource.View.Cast<[your_type]>().Count();

...or...

_viewSource.View.Cast<object>().Count();

...if you don't know the items' type at runtime!


The source collection and collectionview both implements IEnumerable so you can always iterate over them and count how many are in them. But I would only recommend doing this if you have no access to the actual collection you used as source.

private void SetSummary() {    int initialCount = 0;    foreach(var item in _viewSource.View.SourceCollection)    {        initialCount++;    }    int filteredCount = 0;    foreach (var item in _viewSource.View)    {        filteredCount++;    }}