Search code examples
c#wpflistviewoptimizationmvvm

Need help to optimize slow filtering of ListViews


I need help to optimize the filtering into 2 listviews. The 2 grids have the same ItemsSource, and are not displayed in the same time : only as "Cards" or only as grid (Visibility collapsed alternately). If I comment the listview as "Cards" the filtering in the listview as grid is fast. So the slow performance seems to be linked to the ListView as (homemade) "Card" even if its visibility is collapsed. The Listview as Grid is a homemade CustomListView.

I have tried the virtualization on the ListView as "Card" :

 <DataTemplate x:Key="ItemTemplateGroups" xmlns:sys="clr-namespace:System;assembly=mscorlib">
     <VirtualizingStackPanel>
          <Card (...)>
     </VirtualizingStackPanel>
</DataTemplate>

<!-- Groups as Cards -->
<ListView x:Name="lvGroupsAsCards"

          (...)

          Visibility="{Binding vListViewAsCards}"

          VirtualizingStackPanel.IsVirtualizing="True"
          VirtualizingStackPanel.VirtualizationMode="Recycling"
          ScrollViewer.IsDeferredScrollingEnabled="True"
          ScrollViewer.CanContentScroll="True"
          VirtualizingPanel.IsContainerVirtualizable="True"

          ItemTemplate="{StaticResource ItemTemplateGroups}"
          ItemsSource="{Binding ocGroups}"
          SelectedItem="{Binding objSelectedGroup}"
          >

(...)

<!-- Groups as Grid -->
<CustomListView x:Name="lvGroupsAsGrid"
                
                (...)

                Visibility="{Binding vListViewAsGrid}"

                ItemsSource="{Binding ocGroups}"
                SelectedItem="{Binding objSelectedGroup}"
                >

Do you have an idea about how to optimize the header filtering in the homemade CustomListView ?

Thanks per advance for your help :)


Solution

  • VirtualizingStackPanel should be used inside ItemPanelTemplate of ListView. In your case, you are using it inside ItemTemplate which may cause a performance issue since it will create a bunch of instances of VirtualizingStackPanel which may impact the rendering performance.

    Remove it from the ItemTemplateGroups dataTemplate.

     <DataTemplate x:Key="ItemTemplateGroups" xmlns:sys="clr-namespace:System;assembly=mscorlib">
              <Card (...)>
    </DataTemplate>
    

    move VirtualizingStackPanel to the ItemsPanelTemplate

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    

    If it will help you, do not forget to mark my post as the answer:)