检测WPF列表视图滚动条何时位于底部?

时间:2009-08-19 17:22:00

标签: c# wpf listview scroll

有没有办法检测ScrollViewerListView的滚动条是否已到达虚拟滚动空间的底部?我想检测这个以从服务器获取更多项目以放入ObservableCollection上的绑定ListView

现在我正在这样做:

private void currentTagNotContactsList_scrollChanged(object sender, ScrollChangedEventArgs e) {

    ListView v = (ListView)sender;


    if (e.VerticalOffset + e.ViewportHeight == e.ExtentHeight) {
        Debug.Print("At the bottom of the list!");
    }

}

这是否正确?我还需要区分导致事件的垂直滚动条和导致它的水平滚动条(即如果你在框的底部水平滚动,我不想继续生成对服务器的调用)。

感谢。

5 个答案:

答案 0 :(得分:9)

我明白了。看来我应该从ScrollBar(XAML中的<ListView ScrollBar.Scroll="currentTagNotContactsList_Scroll")本身而不是查看器中获取事件。这是有效的,但我只需要想办法避免在滚动条停止后重复调用事件处理程序。也许计时器会很好:

private void currentTagNotContactsList_Scroll(object sender, ScrollEventArgs e) {

    ScrollBar sb = e.OriginalSource as ScrollBar;

    if (sb.Orientation == Orientation.Horizontal)
        return;

    if (sb.Value == sb.Maximum) {
        Debug.Print("At the bottom of the list!");

    }

}

答案 1 :(得分:4)

//A small change in the "Max's" answer to stop the repeatedly call.
//this line to stop the repeatedly call
ScrollViewer.CanContentScroll="False"

private void dtGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
                {
//this is for vertical check & will avoid the call at the load time (first time)
                    if (e.VerticalChange > 0)
                    {
                        if (e.VerticalOffset + e.ViewportHeight == e.ExtentHeight)
                        {
                            // Do your Stuff
                        }
                    }
                }

答案 2 :(得分:2)

对于UWP,我得到了这样的

<ScrollViewer Name="scroll" ViewChanged="scroll_ViewChanged">
    <ListView />
</ScrollViewer>

private void scroll_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
    var scrollViewer = (ScrollViewer)sender;
    if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
            btnNewUpdates.Visibility = Visibility.Visible;
}

答案 3 :(得分:0)

您可以尝试这种方式:

 <ListView ScrollViewer.ScrollChanged="Scroll_ScrollChanged">

和后退:

 private void Scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        // Get the border of the listview (first child of a listview)
        Decorator border = VisualTreeHelper.GetChild(sender as ListView, 0) as Decorator;
        // Get scrollviewer
        ScrollViewer scrollViewer = border.Child as ScrollViewer;
        if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
        Debug.Print("At the bottom of the list!");
    }

答案 4 :(得分:0)

不建议使用 ScrollBar.Scroll ,因为如果你滚动鼠标的中间滚轮,它将不起作用。

<块引用>

ScrollBar.Scroll="currentTagNotContactsList_Scroll"

以下支持右侧滚动条和鼠标滚轮滚动。

在列表框的 xmal 中:

                 ScrollViewer.ScrollChanged="ScrollViewer_ScrollChanged"

在 C# 中:

        private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
            var listBox = (ListBox)sender;
            var scrollViewer =  (ScrollViewer)VisualTreeHelper.GetChild(listBox, 0);

            if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
            {
                Console.WriteLine("____At the bottom of the list!");
            }
    }