我可以在 UWP 中的多个 Listview 项目之间同步滚动吗? [英] Can I sync scrolling between multiple Listview items in UWP?
问题描述
如何链接 2 个 Listview 组件,使它们的滚动同步(当我滚动一个时,另一个也同时滚动)?
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) 监听Listview1的滚动事件2)为Listview2设置相同的滚动偏移
1) Monitor the scrolling event of Listview1 2) Set the same scrolling offset to Listview2
我在 Stackoverflow 上找到了一些示例,但它们指的是 WPF 或 WinRT,它们与我的应用不兼容.
I found some examples here on Stackoverflow but they either refer to WPF, or WinRT and they are not compatible with my app.
谢谢!
推荐答案
您在问题中描述的内容可以通过以下步骤完成:
What you have described in your question can be done by the following steps:
- 在
ListView
中找到ScrollViewer
(从 这个答案). - 订阅
ViewChanged
事件. - 在事件处理程序中,在另一个
ScrollViewer
上调用ChangeView
.
- Find the
ScrollViewer
inside theListView
(check outGetScrollViewer
from this answer). - Subscribe to the
ViewChanged
event. - Inside the event handler, call
ChangeView
on the otherScrollViewer
.
假设您正在垂直滚动 -
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);
};
}
这篇关于我可以在 UWP 中的多个 Listview 项目之间同步滚动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!