Getting a ScrollViewers maximum scroll values?

Viewed 8113

I'm trying to get the maximum amount a scrollviewer can scroll in the vertical and horizontal direction but I need to be doing this in a layout updated callback. This is what I'm currently doing:

        viewer.ScrollToRight( );
        doublehmax = viewer.HorizontalOffset;

        viewer.ScrollToBottom( );
        double vmax = viewer.VerticalOffset;

But this casues an error: "Unhandled Error in Silverlight 2 Application Layout cycle detected. Layout could not complete."

Is there a way I can get the max horizontal and vertical offsets of the scroll view area reliably even after window resizes and the like?

3 Answers

To get max offset you need to check the ScrollViewer's ScrollBar.

ExtentWidth and ScrollableWidth is not what you want, they include the ScrollViewer's ViewportWidth (same with height).

Here's a ScrollViewer extension method to get a ScrollBar reference (because it's part of ScrollViewer's template):

public static ScrollBar GetScrollBar(this ScrollViewer sv, Orientation orientation) {
            if(orientation == Orientation.Vertical) {
                return sv.Template.FindName("PART_VerticalScrollBar", sv) as ScrollBar;
            }
            return sv.Template.FindName("PART_HorizontalScrollBar", sv) as ScrollBar;
        }

Then to get the maximum:

var hScrollBar = viewer.GetScrollBar(Orientation.Horizontal);
double maxOffset = hScrollBar.Maximum;

(This is w/ WPF not Silverlight but googling landed me here so not sure if Template names are the same but you should get the idea)

Related