Synchronise 2 scrollviewers silverlight

南笙酒味 提交于 2019-12-11 03:43:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!