Question

I have a WPF listview and the ScrollViewer shows if large list. The ListView contains items with a checkbox element. We provide the ability to the users to select all and unselect all.

The issue I experienced is only with larger list, meaning we see a subset of the elements and the user needs to use the Scroll Bar to view the rest of the elements. My code for selecting all elements and deselecting all elements is using the VisualTreeHelper helper class to set the property IsChecked of the checkbox to true or false. However it only works for the elements that are visible. For example if I had a collection with 14 checkboxes where 10 checkboxes are visible on the screen, it only selects the 10 that are visible. However, prior to clicking Select All, if you scroll up and down and view the items, then it selects all 14...so it appears that the VisualTree only loads the elements needed. Is there a way to force it to load all elements?

I was able to get it to work by disabling the ScrollViewer on the ListView and surrounding it with a ScrollViewer. Is this the correct approach?

Before:

<ListView x:Name="lstProducts" ItemsSource="{Binding Products}" ItemContainerStyle="{StaticResource STYLE_LISTVIEWITEM}" Grid.Row="1" Margin="4" SelectionMode="Multiple" />

Solution that works:

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" Grid.Row="1">
                    <ListView x:Name="lstProducts" ItemsSource="{Binding Products}" ItemContainerStyle="{StaticResource STYLE_LISTVIEWITEM}" Grid.Row="1" Margin="4" SelectionMode="Multiple" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" />
</ScrollViewer>

Please advise!

Was it helpful?

Solution

try using <ListView VirtualizingStackPanel.IsVirtualizing="false" /> to turn off virtualisation, as the listview containers are getting recycled and only the visible containers exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top