Windows 10 ScrollIntoView() is not scrolling to the items in the middle of a listview

Question!

I have a Listview with 20 items in it. I want to scroll the Listview programmatically.

ListView?.ScrollIntoView(ListView.Items[0])

will scroll the listview to the first item.

ListView?.ScrollIntoView(ListView.Items.Count - 1)

will scroll the listview to the bottom of the page.

However, I am unable to use the same function to scroll the listview to an item in middle.

Eg: ListView?.ScrollIntoView(ListView.Items[5])

should scroll and take me to the 5th item of the list. But instead its taking me to the first item of the list.

Would be great if this behaviour can be achieved with some workaround?

By : Amar Zeno


Answers

I solve this like:

 var sv = new ScrollViewerHelper().GetScrollViewer(listView);
        sv.UpdateLayout();
        sv.ChangeView(0, sv.ExtentHeight, null);

And the GetScrollViewer method:

public ScrollViewer GetScrollViewer(DependencyObject element)
    {
        if (element is ScrollViewer)
        {
            return (ScrollViewer)element;
        }

        for (int i = 0; i 
By : EJL


ScrollIntoView just brings the item into the view, period, it does not scroll to a row.

If you call it on a member and it is below the bottom of the visible list it scrolls down until the the item is the last member in the visible list.

If you call it on a member and it is above the top of the list it scrolls up until the the item is the first member in the list.

If you call it on a member and it is currently visible it does no operation at all.



This video can help you solving your question :)
By: admin