DEV Community

1001binary
1001binary

Posted on

How to get ScrollViewer from ListView in UWP?

Basically, the ScrollViewer is built-in to ListView. Suppose you developed a desktop application based on UWP. In this application, there's one page containing StudentListView. One day you want to scroll it to its top. A first question will be asked that: how to get ScrollViewer from ListView? The following code will answer this question:

XAML

<ListView x:Name="StudentListView"></ListView>
Enter fullscreen mode Exit fullscreen mode

Code Behind

Border border = VisualTreeHelper.GetChild(StudentListView, 0)
as Border;
ScrollViewer scrollViewer = VisualTreeHelper.GetChild(border, 0)
as ScrollViewer;
Enter fullscreen mode Exit fullscreen mode

Next, you can use the following code to scroll StudentListView to its top:

if (scrollViewer != null) {
   scrollViewer.ChangeView(0, 0, 1);
}
Enter fullscreen mode Exit fullscreen mode

Hope this post would be useful for you.

Happy coding :)

Top comments (0)