drmaven
drmaven

Reputation: 47

Can I sync scrolling between multiple Listview items in UWP?

How can I link 2 Listview components, so that their scrolling is in sync (when I scroll one, the other scrolls too at the same time)?

So basically I need a way to:

1) Monitor the scrolling event of Listview1 2) Set the same scrolling offset to Listview2

I found some examples here on Stackoverflow but they either refer to WPF, or WinRT and they are not compatible with my app.

Thank you!

Upvotes: 0

Views: 1418

Answers (1)

Justin XL
Justin XL

Reputation: 39006

What you have described in your question can be done by the following steps:

  1. Find the ScrollViewer inside the ListView (check out GetScrollViewer from this answer).
  2. Subscribe to the ViewChanged event.
  3. Inside the event handler, call ChangeView on the other ScrollViewer.

Assume you are scrolling vertically -

private void SyncScrollViewers()
{
    var scrollViewer1 = MyListView1.GetScrollViewer();
    var scrollViewer2 = MyListView2.GetScrollViewer();

    scrollViewer1.ViewChanged += (s, e) =>
    {
        scrollViewer2.ChangeView(null, scrollViewer1.VerticalOffset, null, false);
    };
}

Upvotes: 3

Related Questions