Scroll Synchronization in WPF and Silverlight
Imagine you have two ListBoxes with lots of items. Whenever a user scrolls in one of the two ListBoxes, the other one should be updated, too. What we want to do in this article is to create a simple attached property, that allows us to group scrollable controls. In the following sample you see two ScrollViewers, whose scroll positions are synchronized because they are both attached to the same ScrollGroup "Group1":
As most scrollable controls use the ScrollViewer in their template to enable scrolling, this should also work for other controls like ListBoxes of TreeViews, as long as they contain a ScrollViewer in their ControlTemplate .
Online Silverlight Demo
Here you can see the Silverlight version of my synchronized ListBoxes:
In the following article I will show, how to build the ScrollSyncronizer class in WPF to synchronize the scroll position of various scrollable controls. In the source code download you will find a working solution for WPF and Silverlight.
- Building the ScrollSynchronizer
- Testing the ScrollSynchronizer
- Synchronizing ListBoxes
- Silverlight Support
Our ScrollSynchronizer object has no representation in the UI. It is just responsible for providing the attached property ScrollGroup. So I have chosen DependencyObject as the base class. First I added the attached dependency property ScrollGroup with its corresponding methodes GetScrollGroup and SetScrollGroup to the class.
In the property metadata of the new property there is a callback that is invoked everytime aScrollViewer uses the attached property, so this is the place where we will provide the logic to synchronize the ScrollViewer with all other attached ScrollViewers. But before we need some private fields to store all attached ScrollViewers as well as their corresponding horizontal and vertical offsets. The string part in these dictionaries is equal to the name of the group that is set by the ScrollGroup property.
Now we can implement the callback for changes in the ScrollGroup property. Basically the code is quite simple. When a new ScrollViewer is added by setting the attached property, we check if we can already find a scroll position for the group in the fieldshorizontalScrollOffset and verticalScrollOffset. If so, we set the adjust the scollposition of the new ScrollViewer, so that it matches the group. Otherwise we add an entry to horizontalScrollOffset and verticalScrollOffset with the current scrollposition of the new ScrollViewer. Finally we add the new ScrollViewer to thescrollViewers dictionary with its corresponding group name, and we add an event handler for the ScrollChanged event, so that we can adapt all other ScrollViewers in the group when the scrollposition has changed.
If the attached property is removed we remove the ScrollViewer from the list. In this case we do not remove the entries in horizontalScrollOffset and verticalScrollOffset, even when it is the last ScrollViewer of one group, because when another ScrollViewer is added to that group later, we still know the last scrollposition of that group.
Now our last task is to implement the event handler for the ScrollChanged event. If the horizontal or the vertical scrollposition has changed, we update the dictionariesverticalScrollOffsets and horizontalScrollOffsets to the latest position. Then we have to find all ScrollViewers that are in the same group as the changed ScrollViewer and update its scroll positions.
To test the new attached property we build a simple UI with two ScrollViewers. For both ScrollViewers we assign the value "Group1" to the ScrollGroup property.
In the code-behind file we add some TextBlocks to both panels, so that the ScrollBars will get visible.
Done! We have two synchronized ScrollViewers:
Now, how can we get other controls synchronized? Let's replace the ScrollViewers by two ListBoxes. Unfortunately we cannot set the attached property ScrollGroup to the ListBoxes. In the OnScrollGroupChanged callback we assume, that we will always get a ScrollViewer. So we could enhance the ScrollSynchronizer to accept other types of controls, or we could simply add a style for the ScrollViewer within the ListBoxes, that sets the ScrollGroupproperty. In this case no changes are necessary for out ScrollSynchronizer.
A nicer way to do this would be to set the style in the Grid resources, so it applies to all ScrollViewers in the grid automatically.
Basically this solution would also work for Silverlight. In detail there are some differences like a ScrollViewer does not provide the ScrollChanged event in Silverlight. But you can bypass this problem by using the Scroll and ValueChanged events of the underlying ScrollBars. Another problem is that the Style for the ScrollViewer is not applied in the ListBox sample, even when using the ImplicitStyleManager. So I ended up setting the attached property in code for Silverlight. In the source code download you will find a working solution for WPF and Silverlight.