ListView.ScrollIntoView(object) currently finds an object in the ListView and scrolls to it. If you are positioned beneath the object you are scrolling to, it scrolls the object to the top row. If you are positioned above, it scrolls it into view at the bottom row.
I’d like to have the item be scrolled right into the center of my list view if it is currently not visible. Is there an easy way to accomplish this?
It is very easy to do this in WPF with an extension method I wrote. All you have to do to scroll an item to the center of the view is to call a single method.
Suppose you have this XAML:
Your ScrollIntoView method will be simply:
Obviously this could be done using a ViewModel as well rather than referencing the controls explicitly.
Following is the implementation. It is very general, handling all the IScrollInfo possibilities. It works with ListBox or any other ItemsControl, and works with any panel including StackPanel, VirtualizingStackPanel, WrapPanel, DockPanel, Canvas, Grid, etc.
Just put this in a .cs file somewhere in your project: