每当在 wpf 中滚动任何一个时,两个 ScrollViewer 的同步滚动 [英] Synchronized scrolling of two ScrollViewers whenever any one is scrolled in wpf
问题描述
我已经完成了线程:
它几乎帮助实现了目标,但仍然缺少一些东西.正是左右或上下移动滚动条会在我的两个滚动查看器中提供预期的滚动行为,但是当我们尝试使用/单击滚动查看器中这些滚动条末端的箭头按钮滚动时,只有一个滚动查看器被滚动,这不是预期的行为.
it has almost helped to achieve the goal but still there is something missing. It is that moving the scrollbars left-right or up-down gives expected behavior of scrolling in both of my scrollviewers but when we try to scroll using/clicking arrow buttons at the ends of these scrollbars in scrollviewers only one scrollviewer is scrolled which is not the expected behavior.
那么我们还需要添加/编辑什么来解决这个问题?
So what else we need to add/edit to solve this?
推荐答案
一种方法是使用 ScrollChanged
事件来更新另一个 ScrollViewer
One way to do this is using the ScrollChanged
event to update the other ScrollViewer
<ScrollViewer Name="sv1" Height="100"
HorizontalScrollBarVisibility="Auto"
ScrollChanged="ScrollChanged">
<Grid Height="1000" Width="1000" Background="Green" />
</ScrollViewer>
<ScrollViewer Name="sv2" Height="100"
HorizontalScrollBarVisibility="Auto"
ScrollChanged="ScrollChanged">
<Grid Height="1000" Width="1000" Background="Blue" />
</ScrollViewer>
private void ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (sender == sv1)
{
sv2.ScrollToVerticalOffset(e.VerticalOffset);
sv2.ScrollToHorizontalOffset(e.HorizontalOffset);
}
else
{
sv1.ScrollToVerticalOffset(e.VerticalOffset);
sv1.ScrollToHorizontalOffset(e.HorizontalOffset);
}
}
这篇关于每当在 wpf 中滚动任何一个时,两个 ScrollViewer 的同步滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!