问题
I want to synchronize 2 scrollviewers. Please let me know how to get scroll event of both scrollviewers and then synchronize them both?
回答1:
First get the 2 scrollbars of the scrollviewers you want to sync.
In this case, scrollviewer1
and scrollviewer2
Then we get event handlers of both the scrollbars, in this case vertical. Then we can easily sync them through the events. The ScrollToVerticalOffset
will scroll as per the other one does.
ScrollBar vertical1 = ((FrameworkElement)VisualTreeHelper.GetChild(scrollviewer1, 0)).FindName("VerticalScrollBar") as ScrollBar;
vertical1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(vertical1_ValueChanged);
ScrollBar vertical2 = ((FrameworkElement)VisualTreeHelper.GetChild(scrollviewer2, 0)).FindName("VerticalScrollBar") as ScrollBar;
vertical2.ValueChanged += new RoutedPropertyChangedEventHandler<double>(vertical2_ValueChanged);
void vertical1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
scrollviewer2.ScrollToVerticalOffset(e.NewValue);
}
void vertical2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
scrollviewer1.ScrollToVerticalOffset(e.NewValue);
}
Hope this helps!
来源:https://stackoverflow.com/questions/10849524/synchronise-2-scrollviewers-silverlight