两个ScrollViewers同步滚动,只要任何一个滚动WPF [英] Synchronized scrolling of two ScrollViewers whenever any one is scrolled in wpf
问题描述
我已经通过线了:
<一个href="http://stackoverflow.com/questions/5957138/binding-two-verticalscrollbars-one-to-another">binding 2 VerticalScrollBars一个到另一个
这几乎帮助实现这一目标,但仍然有一些人失踪。这是那动人的左右或上下滚动条给出预期滚动在我的两个scrollviewers但行为时,我们试图滚动使用/点击箭头按钮,这些滚动条在scrollviewers只有一个的ScrollViewer滚动的目的是不预期的行为。
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);
}
}
这篇关于两个ScrollViewers同步滚动,只要任何一个滚动WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!