如何检查WPF DataGrid中当前是否可以看到滚动?

时间:2010-10-21 11:43:19

标签: c# wpf datagrid scroll visibility

如何检查WPF DataGrid中当前是否显示滚动(垂直或水平)? Horizo​​ntalScrollBarVisibility和VerticalScrollBarVisibility用于设置行为,并将它们设置为Auto。

3 个答案:

答案 0 :(得分:23)

ScrollViewer scrollview = FindVisualChild<ScrollViewer>(dataGrid);
Visibility verticalVisibility = scrollview.ComputedVerticalScrollBarVisibility;
Visibility horizontalVisibility = scrollview.ComputedHorizontalScrollBarVisibility;

答案 1 :(得分:10)

这是FindVisualChild的一个实现:

private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is childItem)
            return (childItem)child;
        else
        {
            childItem childOfChild = FindVisualChild<childItem>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}

答案 2 :(得分:0)

Private Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
            If child IsNot Nothing AndAlso TypeOf child Is childItem Then
                Return CType(child, childItem)
            Else
                Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)
                If childOfChild IsNot Nothing Then
                    Return childOfChild
                End If
            End If
        Next i
        Return Nothing
    End Function