检测,如果ScrollViewer的ScrollBar可见或不可见

Ben*_*ual 12 c# wpf scrollviewer

我有一个TreeView.现在,我想检测,如果垂直滚动条是否可见.我试试的时候

var visibility = this.ProjectTree.GetValue(ScrollViewer.VerticalScrollBarVisibilityProperty)
Run Code Online (Sandbox Code Playgroud)

(其中this.ProjectTree是TreeView)我总是获得Auto以获得可见性.

如果ScrollBar有效可见,我该如何检测?

谢谢.

Tho*_*que 16

您可以使用该ComputedVerticalScrollBarVisibility物业.但为此,您首先需要ScrollViewerTreeView模板中找到它.为此,您可以使用以下扩展方法:

    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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

var scrollViewer = ProjectTree.GetDescendants().OfType<ScrollViewer>().First();
var visibility = scrollViewer.ComputedVerticalScrollBarVisibility;
Run Code Online (Sandbox Code Playgroud)

  • @malthe,我认为没有特定的事件,但是由于它是一个依赖项属性,您可以随时使用`DependencyPropertyDescriptor.AddValueChanged`观看它 (2认同)