1

How can I programatically determine the width or height of a Scrollbar in the ScrollViewer? Not the current size based on the current state of the ScrollViewer (since I can simply test the visibility of the scrollbar and calculate against the ViewerPortWidth/Height and the ScrollViewer ActualWidth/Height).

I need to know what size the Scrollbar's could be based on the templated width. The default is 18 (with a margin of -1). But we all know what could happen if I use the magic number of 18 in my calculations. Some of my users could template the Scrollbars in the ScrollViewer then I'd be screwed.

Thanks!

beaudetious
  • 2,354
  • 3
  • 36
  • 60
  • Hi, sorry, I don't understand the problem - why can't you use e.g. (ActualWidth - ViewPortWidth) ? – alexander.biskop May 21 '10 at 15:20
  • possible duplicate of [Determine the width of the vertical scroll bar in a ScrollViewer](http://stackoverflow.com/questions/2777384/determine-the-width-of-the-vertical-scroll-bar-in-a-scrollviewer) – Lance Roberts Aug 24 '11 at 00:56
  • It's not duplicate, there's no such SystemParameters properties in Silverlight – olegz Feb 13 '12 at 07:00

2 Answers2

2

You can use SystemParameters.ScrollWidth.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
metao
  • 2,784
  • 1
  • 18
  • 15
1

You can use Silverlight Toolkit's VisualTreeHelper in the following way:

var verticalScrollbar = view.GetVisualChildren().OfType<FrameworkElement>()
        .FirstOrDefault(e => e.Name == "VerticalScrollBar");
var horizontalScrollbar = view.GetVisualChildren().OfType<FrameworkElement>()
        .FirstOrDefault(e => e.Name == "HorizontalScrollBar");
var width = verticalScrollbar == null ? 0 : verticalScrollbar.ActualWidth;
var height = horizontalScrollbar == null ? 0 : horizontalScrollbar.ActualHeight;

where view is an instance of ScrollViewer (I guess you've got the instance).

olegz
  • 1,189
  • 10
  • 20