34

As most WPF developers know, setting ScrollViewer.CanContentScroll to false will disable virtualization; but I'd like to know how it works, because I try to enable virtualization while setting ScrollViewer.CanContentScroll to false.

Adi Lester
  • 23,781
  • 12
  • 86
  • 106
Park Wu
  • 341
  • 1
  • 3
  • 3

3 Answers3

50

"ScrollViewer currently allows two scrolling modes: smooth pixel-by-pixel scrolling (CanContentScroll = false) or discrete item-by-item scrolling (CanContentScroll = true). Currently WPF supports UI virtualization only when scrolling by item. Pixel-based scrolling is also called “physical scrolling” and item-based scrolling is also called “logical scrolling”."

Virtualization requires an item-based scrolling so it can keep track of logical units (items) currently in view... Setting the ScrollViewer to a pixel-based scrolling there is no more concept of logic units but only pixels!!!

Parisa
  • 136
  • 11
rudigrobler
  • 16,589
  • 12
  • 58
  • 73
7

UI Virtualization

I’m often asked if there is a way to work around this limitation. Well, anything is possible, but there is no easy workaround. You would have to re-implement significant portions of the current virtualization logic to combine pixel-based scrolling with UI virtualization. You would also have to solve some interesting problems that come with it. For example, how do you calculate the size of the thumb when the item containers have different heights? (Remember that you don’t know the height of the virtualized containers – you only know the height of the containers that are currently displayed.) You could assume an average based on the heights you do know, or you could keep a list with the item heights as items are brought into memory (which would increase accuracy of the thumb size as the user interacts with the control). You could also decide that pixel-based scrolling only works with items that are of the same height – this would simplify the solution. So, yes, you could come up with a solution to work around this limitation, but it’s not trivial.

Glenn Slayden
  • 14,572
  • 3
  • 90
  • 97
egoroveo
  • 188
  • 1
  • 6
4

You can restore the virtualization with VirtualizingPanel.ScrollUnit="Pixel" (in .NET >= 4.5).

Hoddmimes
  • 71
  • 4
  • Hi @Hoddmimes I tried it but it doesn't work, is there anything else needed to turn it ON to work on a pixel-based unit? I tried set `VirtualizingPanel.ScrollUnit="Pixel"` and `VirtualizingPanel.CacheLengthUnit="Pixel"`, and other properties such as `CacheLength` but nothing works – Puty May 10 '20 at 07:39
  • 1
    Hi @Hoddmimes, I solved it, I set in ListView: `ScrollViewer.CanContentScroll="True"`, ` VirtualizingPanel.IsVirtualizing="True"`, ` VirtualizingPanel.ScrollUnit="Pixel"` and `VirtualizingPanel.IsContainerVirtualizable="True"`, and add: ` ` and it works. – Puty May 11 '20 at 08:14
  • This actually works! Here is my [example](https://gist.github.com/PiN73/1559e6c940dae092a722e71746b87160) with `` – Pavel Feb 14 '21 at 16:54