'Getting a ScrollViewers maximum scroll values?
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?
Solution 1:[1]
I think what you want is the ExtentWidth and ExtentHeight, right? You want the max size of what's inside the ScrollViewer.
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ed Bayiates |
| Solution 2 | uno_1_herman |
