HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Wiring CollectionView and grouping it without slowing it down

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withoutgroupingslowingdownwiringandcollectionview

Problem

In my previous question, I got heaps of good advice BUT no solution for the problem apposed.

I applied the valuable learnings from the past on this.question and am also hitting it from a different angle.

The second part is the reason I didn't mark the question as 2.0 version of the first but feel free to let me know how to fix the overall CollectionView wiring.

The main issue here is speed, speed & speed. Everything is behaving and lightning fast until I add grouping functionality to the ListView's CollectionView and from saying that I mean the list takes way longer to load and after it does, functionality(s) such as filtering and searching (filtering) are not usable anymore. I believe this is partially due to using Refresh() as Dr.WPF points out:


Unfortunately, the Refresh() method results in a complete regeneration of the view. It is a rather drastic operation, to say the least. Furthermore, when a Refresh() occurs within the view, it raises a CollectionChanged notification and supplies the Action as “Reset”.

And also grouping itself as @scobi says in response to this SO answer,


Note that VirtualizingStackPanels are the default items panel template for ListViews. Using some features like grouping will override the default, however.

which all of a sudden it becomes clear why grouping bogs my ListView down.

Now I know what's the problem (I think), and looking for a solution to improve the code and speed it up.

AllPartsViewModel.cs

```
public class AllPartsViewModel : WorkspaceViewModel
{
#region Fields

readonly RelayCommand _clearSearch;
readonly PartRepository _partRepository;
readonly VendorRepository _vendorRepository;
bool _errorParts;
private bool _priceSurgeMedium;
private bool _priceSurgeHigh;
List allpvms;
RelayCommand _exportToCsv;
INotifyCollectionChanged notifyCollectionChanged;
string _searchFilterString;

#endregion // Fields

#region Constructor

public AllPartsViewMo

Solution

I have extensively studied this issue during the past week and this is the result for how to improve the speed in the ListView when Grouping is activated.


In a normal WPF ItemsControl that virtualizes, such as ListBox or
ListView, virtualization turns off when you turn grouping on.

And using .NET 4.0, I was unable to find any reasonable solution other than some really messy ones like this or even using TreeListView instead.

In the higher versions of .NET such as 4.5 there is a property introduced called VirtualizingPanel.IsVirtualizingWhenGrouping which might be able to fix the performance issue of the ListView. And I believe it is more than possible as the MSDN description says:


Gets or sets a value that indicates whether this VirtualizingPanel
virtualizes the items in its collection when it displays groups.

So the final conclusion is that, if you're using .NET 4 and lower, use grouping only with the small amount of members to manage. If you go data in thousands or even in hundreds range, you'd be looking at non standard alternatives or simply not using the grouping feature.

Update

I have recently bumped up my .NET version from 4.0 to 4.5 and following that I used VirtualizingPanel.IsVirtualizingWhenGrouping as shown in the code below and the results are amazing. Grouping works very smooth and other features such as search and filtering won't get effected at all.

`

Context

StackExchange Code Review Q#77674, answer score: 7

Revisions (0)

No revisions yet.