none
wpf 如何判断ListView自带的滚动条是否到达最顶端或者最低端或者没有滚动条? RRS feed

答案

  • 您好 轮回的锯齿,

    要做这些判断,我们首先需要通过VisualTreeHelper来把ListView内部的ScrollViewer控件实例获取到。

    //获取视觉树上的控件
    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;
    }
    ScrollViewer scrollviewer = FindVisualChild<ScrollViewer>(listView);

    有个这个实例后,我们就可以通过它内部的一些属性来判断你想要的结果。

    //获得滚动条是否显示
    Visibility verticalVisibility = scrollviewer.ComputedVerticalScrollBarVisibility;
    Visibility horizontalVisibility = scrollviewer.ComputedHorizontalScrollBarVisibility;
    //如何VerticalOffset和ScrollableHeight相等表明已经到最底了
    if (scrollviewer.VerticalOffset == scrollviewer.ScrollableHeight)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年10月5日 2:40
  • 您好 轮回的齿轮,

    >>"判断滚动条到最顶端的条件你还没有回答呢,难道没有这个条件么?"

    判断滚动条是否到达顶端可以判断scrollviewer.VerticalOffset是否为0.

    >>"//这段代码是什么意思?这是一个方法的声明吧?“childItem FindVisualChild<childItem>” 这两段英文是方法返回类型吧?,"

    这是一个泛型方法,其中childItem是个类型占位符。由调用此方法的地方传递过来。关于泛型方法,你可以参考以下链接。

    https://msdn.microsoft.com/zh-cn/library/twcad0zb(v=vs.140).aspx

    >>"后面的 where childItem : DependencyObject整体看起来是什么意思?where是linq里的where?"

    后面的where是对泛型类型的限制,是泛型中的特定的关键字,表示强制要求传递过来的类型必须继承自DependencyObject。

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年10月6日 1:17

全部回复

  • 您好 轮回的锯齿,

    要做这些判断,我们首先需要通过VisualTreeHelper来把ListView内部的ScrollViewer控件实例获取到。

    //获取视觉树上的控件
    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;
    }
    ScrollViewer scrollviewer = FindVisualChild<ScrollViewer>(listView);

    有个这个实例后,我们就可以通过它内部的一些属性来判断你想要的结果。

    //获得滚动条是否显示
    Visibility verticalVisibility = scrollviewer.ComputedVerticalScrollBarVisibility;
    Visibility horizontalVisibility = scrollviewer.ComputedHorizontalScrollBarVisibility;
    //如何VerticalOffset和ScrollableHeight相等表明已经到最底了
    if (scrollviewer.VerticalOffset == scrollviewer.ScrollableHeight)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年10月5日 2:40
  • 判断滚动条到最顶端的条件你还没有回答呢,难道没有这个条件么?

    另外

    private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject

    //这段代码是什么意思?这是一个方法的声明吧?“childItem FindVisualChild<childItem>” 这两段英文是方法返回类型吧?,

    后面的 where childItem : DependencyObject整体看起来是什么意思?where是linq里的where?

    冒号后面的类名是否表示方法可以继承类?


    2016年10月5日 10:14
  • 您好 轮回的齿轮,

    >>"判断滚动条到最顶端的条件你还没有回答呢,难道没有这个条件么?"

    判断滚动条是否到达顶端可以判断scrollviewer.VerticalOffset是否为0.

    >>"//这段代码是什么意思?这是一个方法的声明吧?“childItem FindVisualChild<childItem>” 这两段英文是方法返回类型吧?,"

    这是一个泛型方法,其中childItem是个类型占位符。由调用此方法的地方传递过来。关于泛型方法,你可以参考以下链接。

    https://msdn.microsoft.com/zh-cn/library/twcad0zb(v=vs.140).aspx

    >>"后面的 where childItem : DependencyObject整体看起来是什么意思?where是linq里的where?"

    后面的where是对泛型类型的限制,是泛型中的特定的关键字,表示强制要求传递过来的类型必须继承自DependencyObject。

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年10月6日 1:17