1
votes

In my graphics application, there is canvas area that has some graph. The scrollbars associated with the x- cordinate and y-cordinate must be in sync with this viewport area of the canvas (where the graph is present). When a user clicks on a zoom button the graph is zoomed in and out. Now the problem is setting the scrollbar for this zooming. On zoomin in or out, the viewport of the scrollbar should be in sync with the percentage of zoom.

How to set the thumb size of the scrollbar in this case. I have the Maximum , Minimum and Value property of the scrollbar and the entire graph width and height with the zoomfactor.

How should I set the ViewportSize or the thumb size of the scrollbar?

I used the below code snippet to get the ViewportSize but its not accurate

mScrollBarY.ViewportSize = (double)yAxis.CurrentValueRange;

where the CurrentValueRange gives the zoomed in visible range.

1
Couldn't you handle all this automatically by applying a LayoutTransform to the Canvas and putting it in a ScrollViewer?Clemens

1 Answers

3
votes

Here I see two approaches:

  1. Viewport remaines the same, while Extent grows as you zoom in and decreases down to Viewport size as you zoom out. So you need to update Maximum
  2. Extent remains the same, while Viewport grows up to Extent size as you zoom out and decreases as you zoom in.

EDIT

In both variants you need to define some factor to increase or decrease by. Let it be scale_factor. Here's the method to update ScrollBar:

void updateScrollBar()
{
    scrollBar.Minimum = 0;
    scrollBar.Maximum = extent;
    scrollBar.ViewportSize = viewport * scale_factor;
    scrollBar.SmallChange = 10 * scale_factor;
}

Zoom In handler:

scale_factor /= 2;
updateScrollBar();

Zoom out handler:

scale_factor *= 2;
updateScrollBar();

By the way, I found the following formula at msdn forum:

ThumbSize = TrackLength * ViewportSize / (Maximum - Minimum + ViewportSize)