Changing exact scroll-position in a Listview

c#, wpf

Solution

There is no way of doing that using the `ListView` element. Instead, you need to access its `ScrollViewer` and then you can use the `ScrollViewer.ScrollToVerticalOffset` Method to set the vertical position of the `ScrollViewer`. You'll also need to use the `ScrollViewer.VerticalOffset`, `ScrollViewer.VerticalOffset`, `ScrollViewer.ViewportHeight` and `ScrollViewer.ExtentHeight` properties to gauge where you are in the `ScrollViewer`.

From the `ScrollViewer` Class page on MSDN:

The area that includes all of the content of the ScrollViewer is the extent. The visible area of the content is the viewport.

Finally, how do you get the `ScrollViewer` from the `ListView`? I can't guarantee that this will work on a `ListView`, but it does on a `ListBox`. you can use the `VisualTreeHelper.GetChild` Method to delve into the visual tree of the `ListView` and it should contain a `Border` and then the `ScrollViewer`, so you should be able to do something like this:

Border border = (Border)VisualTreeHelper.GetChild(YourListView, 0);
ScrollViewer scrollViewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
if (scrollViewer != null)
{
    scrollViewer.ScrollToVerticalOffset(60.0);
}

If you do get an error with what the `GetChild` method returns, it'll be easy to adjust by debugging it. Just put a breakpoint there and see what each child type is and add another line with one of those elements... eventually, it should find the `ScrollViewer`. However, I think that that code should be fine.

Problem

Been searching around the net for the answer but I haven't found anything that does this: I want to change the exact position in a WPF-list iew programmatically. Some way of saying ``` ListView.Scrollposition.Y = some value; ``` The only thing I can find is to change the value to a object within the Listview, not specific coordinates. If anyone has some good articles or posts in this subject, I'd be very grateful! Thanks.

Original source