18

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.

1
  • Out of curiosity, why do you need to do this? In most cases where I've seen someone doing this, it was a sub-optimal way of solving some other problem. Nov 18, 2013 at 14:50

3 Answers 3

23

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;
3
  • Can you attach a property changed event to this property?
    – malthe
    Jan 17, 2015 at 7:38
  • 4
    @malthe, I don't think there's a specific event for this, but since it's a dependency property, you can always watch it using DependencyPropertyDescriptor.AddValueChanged Jan 17, 2015 at 12:10
  • Note that if you're trying to use ComputedVerticalScrollBarVisibility on a Scrollviewer then make sure VerticalScrollBarVisibility="Auto" in xaml
    – Shai UI
    Jun 27, 2017 at 17:51
3

ComputedVerticalScrollBarVisibility instead of VerticalScrollBarVisibility

VerticalScrollBarVisibility sets or gets the behavior, whereas the ComputedVerticalScrollBarVisibility gives you the actual status.

http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.computedverticalscrollbarvisibility(v=vs.110).aspx

You cannot access this property the same way you did in your code example, see Thomas Levesque's answer for that :)

3
  • 2
    Yes, but it's a property of the ScrollViewer, not the TreeView, so you need to find the ScrollViewer in the template first. Nov 18, 2013 at 13:08
  • I assumed that if the code in the OP returned a result, then it successfully addressed the ScrollViewer allready, just the wrong property...
    – oerkelens
    Nov 18, 2013 at 13:10
  • No, because ScrollViewer.VerticalScrollBarVisibility is an attached property, which is set on the TreeView, not the ScrollViewer. Nov 18, 2013 at 13:25
3

Easiest approach I've found is to simply subscribe to the ScrollChanged event which is part of the attached property ScrollViewer, for example:

<TreeView ScrollViewer.ScrollChanged="TreeView_OnScrollChanged">
</TreeView>

Codebehind:

private void TreeView_OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (e.OriginalSource is ScrollViewer sv)
    {
        Debug.WriteLine(sv.ComputedVerticalScrollBarVisibility);
    }
}

For some reason IntelliSense didn't show me the event but it works.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.