DEV Community

1001binary
1001binary

Posted on

4 2

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay