Hello,
I came to interesting problem of calculating correct number of items that are grouped up. I have GridView that has grouped items, however in each group there is "special" item, that points to object of different type (but derived from the base type of the ItemsSource collection). This item should not be counted into overall item count of the group, as its function is to enable user to write down some notes for each group and does not contain any production data. The calculated count is then displayed in the group header text.
My idea was to create converter, that would take Group object and return the number. The converter itself would take group.ItemCount of group and then iterate in loop over the group.Items collection and substract 1 from the count if the item is of specified type. All of this is done so that when the user searches via the inbuilt search bar in GridView, the count is always correct.
Now the problem, the gridview does no load items of groups that are not expanded (except for the first one??) and as such the converter iterates over nothing (the group.Items collection is empty) and thus the count is displayed wrong (displayed number is group.ItemCount without any substraction). Any ideas how could i work around this? I would rather avoid calculating this in viewmodel as that would require tracking the search text and then manually filtering items in collection.
Hi Christofer, I couldn't get a behavior where the group.Items collection is empty when the group is not expanded. I've attached a sample project showing my approach to the described requirement. I hope this will be helpful for your scenario.
In the project, I've decided to use reflection in order to get one of the internal properties which allows me to get a flat collection of all child items, instead of writing extra code for this.