Scrollbar Size in ScrollViewer

隐身守侯 提交于 2019-12-11 05:16:33

问题


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!


回答1:


You can use SystemParameters.ScrollWidth.




回答2:


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).



来源:https://stackoverflow.com/questions/2866586/scrollbar-size-in-scrollviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!