Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect, if ScrollBar of ScrollViewer is visible or not

I have a TreeView. Now, I want to detect, if the vertical Scrollbar is visible or not. When I try it with

var visibility = this.ProjectTree.GetValue(ScrollViewer.VerticalScrollBarVisibilityProperty)

(where this.ProjectTree is the TreeView) I get always Auto for visibility.

How can I do this to detect, if the ScrollBar is effectiv visible or not?

Thanks.

like image 389
BennoDual Avatar asked Nov 18 '13 12:11

BennoDual


1 Answers

You can use the ComputedVerticalScrollBarVisibility property. But for that, you first need to find the ScrollViewer in the TreeView's template. To do that, you can use the following extension method:

    public static IEnumerable<DependencyObject> GetDescendants(this DependencyObject obj)
    {
        foreach (var child in obj.GetChildren())
        {
            yield return child;
            foreach (var descendant in child.GetDescendants())
            {
                yield return descendant;
            }
        }
    }

Use it like this:

var scrollViewer = ProjectTree.GetDescendants().OfType<ScrollViewer>().First();
var visibility = scrollViewer.ComputedVerticalScrollBarVisibility;
like image 66
Thomas Levesque Avatar answered Sep 30 '22 02:09

Thomas Levesque