Pivot grid in WPF

By Mirek on (tags: pivot grid, WPF, categories: code)

Today I am going to show you how easy to achieve a pivot scrolling in wpf using scroll viewers and scroll bars.

What we are going to achieve is presented on following picture

The center and top part can be scrolled horizontally using bottom scroll bar (red rectangle). From the other side the vertical scroll bar affects center and left side part (orange rectangle). So when we scroll vertically the top part doesn’t move and when we scroll horizontally the left part doesn’t move. The xaml of the window consists of three scroll viewers and two scroll bars.

Now the goal is to have the scroll viewers react on scroll bars change and from the other side scrolling on scroll viewer should adjust the value of proper scroll bar. Let’s do it by binding. The ScrollBar has a dependency property Value which we can bind to. The ScrollViewer has HorizontalOffset and VerticalOffset which both represents the scroll value of the viewer, however both those are not dependency property, so we cannot bind to them.
The solution is to extend the ScrollViewer and expose horizontal and vertical offsets as dependency properties.

Having that we can bind our scroll bar’s Value to the HorizontalOffsetBindable and VerticalOffsetBindable. For instance for the center scroll viewer we must bind both scroll bars, the vertical and horizontal one.

The dependency properties defined in ScrollViewerEx are set to be TwoWay by default. This makes the scrolling working from both ways. Either we scroll on the viewer with use for instance mouse wheel or we drag and move the scroll bar, the content of the viewer is scrolled.  Additionally to have the scroll bars reflect the vieport size we must set its Maximum and ViewportSize properties to corresponding properties of the center scroll viewer

 

And we are done. The complete solution can be found in attachement.