Detect, if ScrollBar of ScrollViewer is visible or not

c#, scrollviewer, wpf

Solution

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;

Problem

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.

Original source