I do have a listbox which must have CanContentScroll==false because i need to be able to scroll it smoothly. This enables physical scrolling.
I also want to scroll the listbox by page, but if i call the PageDown method on the listbox internal ScrollViewer the first row gets cutted because the listbox height is not a multiple of the row height.
I want the first row to be always completely visible, like when using logical scrolling.
Can someone give me a hint on how to do that?
You’ll get the same effect for a non virtualizing
ItemsControl(ScrollViewer.CanContentScroll="False") as for a virtualizing one if you scroll down and then select the upper visible container with the mouse. This can also be done in code.When
CanContentScrollis set to false, virtualizing is turned off so all containers will be generated at all times. To get the top visible container we can iterate the containers from the top until we reach theVerticalOffsetof theScrollViewer. Once we got it we can simply callBringIntoViewon it and it will align nicely at the top just like it would if virtualization was being used.Example
Call
BringIntoViewon the top visible container in the event handlerTo only achieve this effect when you want to call
PageDown, in a Button click for example, you can create an extension method forListBoxcalledLogicalPageDown.ListBoxExtensions
I noticed in your question that you already got the
ScrollViewerbut I’m adding an implementation toGetVisualChildif anyone else comes across this question