本文介绍了检测 ScrollViewer 的 ScrollBar 是否可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树视图.现在,我想检测垂直滚动条是否可见.当我尝试使用

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)

(这里的 this.ProjectTree 是 TreeView)为了能见度,我总是使用 Auto.

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

如何检测 ScrollBar 是否有效可见?

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

谢谢.

推荐答案

您可以使用 ComputedVerticalScrollBarVisibility 属性.但为此,您首先需要在 TreeView 的模板中找到 ScrollViewer.为此,您可以使用以下扩展方法:

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;
            }
        }
    }

像这样使用它:

var scrollViewer = ProjectTree.GetDescendants().OfType<ScrollViewer>().First();
var visibility = scrollViewer.ComputedVerticalScrollBarVisibility;

这篇关于检测 ScrollViewer 的 ScrollBar 是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:04