How to get Items count from CollectionViewSource?
Asked Answered
W

6

19

I am using CollectionViewSource to filter the records displayed in a ListBox. The xaml follows.

   <Window x:Class="WPFStarter.ListBoxItemsFilter.ListBoxFilterUsingCollectionViewSource"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="userControl"
        Title="ListBoxFilterUsingCollectionViewSource" Height="300" Width="300">
        <Window.Resources>
        <CollectionViewSource Source="{Binding ElementName=userControl, Path=DataContext.Items}"
                              x:Key="cvs" Filter="CollectionViewSource_Filter"/>
        </Window.Resources>
    <StackPanel Orientation="Vertical">
        <TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged"/>
        <TextBlock x:Name="txtSummary" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Bottom"  FontSize="8"></TextBlock>
        <ListBox ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="First"/>
    </StackPanel>

</Window>

And here is my code-behing ( please don;t mind this code-behind, in the real application i am using the best of MVVM for this scenario).

 public partial class ListBoxFilterUsingCollectionViewSource : Window
    {
        private string _text="";
        private readonly CollectionViewSource _viewSource;

        public ListBoxFilterUsingCollectionViewSource()
        {
            InitializeComponent();
            _viewSource = this.FindResource("cvs") as CollectionViewSource;
        }

        private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
        {
            var character = e.Item as Character;
            e.Accepted = character != null && character.First.ToLower().Contains(_text.ToLower());
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _text = txtSearch.Text;
            _viewSource.View.Refresh();

            SetSummary();
        }

        private void SetSummary()
        {                
            var initialCount = 10; //HELP????
            var filteredCount = 10; //HELP????
            txtSummary.Text = String.Format("{0} of {1}", filteredCount, initialCount);
        }
    }

QUESTION: I Need help in writing the "SetSummary" method, wherein i can get the "initialCount" and the "filteredCount" from CollectionViewSource object.

Thanks for your interest.

Widgeon answered 17/8, 2010 at 6:39 Comment(0)
C
7

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++;
    }
}
Convulse answered 17/8, 2010 at 21:13 Comment(1)
Just a note that the filteredCount is the number if visbile items not a count of the items that have been filtered out.Epidaurus
A
46

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

Azurite answered 5/1, 2011 at 8:31 Comment(1)
Thank you for clarifying how to get at the filtered/sorted contents of a CollectionViewSource. I was unable to get the first item in the CollectionViewSource until your comment: var firstItem = this.xViewInDescendingOrder.View.Cast<Thing>().ElementAt<Thing>(0);Hymettus
P
12

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!

Paroicous answered 9/7, 2012 at 15:13 Comment(0)
C
7

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++;
    }
}
Convulse answered 17/8, 2010 at 21:13 Comment(1)
Just a note that the filteredCount is the number if visbile items not a count of the items that have been filtered out.Epidaurus
U
4

If you're doing MVVM, you could have your VM create a collection view rather than one being created on your behalf by the CollectionViewSource. Then, you have control over what type of CVS is created, so you can create a ListCollectionViewSource, which has a Count property. It really depends on the properties of the data you're filtering.

Urbai answered 17/8, 2010 at 9:57 Comment(1)
Sorry i could not get it. Can you please explain with some code by taking my code in the context. Thanks.Widgeon
G
2
var count = DataGrid.ItemsSource.OfType<object>().Count();
Gilberto answered 11/12, 2016 at 20:52 Comment(3)
Through this you can get counted number of itemsSource e.g 2 , 3Gilberto
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.Dissenter
Thanks Mr. Robert Columbia for guiding me to helping in good waysGilberto
Z
0
public static int Count(this ICollectionView view)
    {
        var index = 0;
        foreach (var unused in view)
        {
            index++;
        }
        return index;
    }
Zarate answered 20/5, 2018 at 19:26 Comment(1)
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.Dissenter

© 2022 - 2024 — McMap. All rights reserved.