Detect when WPF listview scrollbar is at the bottom?

前端 未结 4 882
别那么骄傲
别那么骄傲 2020-12-17 17:17

Is there a way to detect if the scrollbar from the ScrollViewer in a ListView has reached the bottom of the virtual scroll space? I would like to

相关标签:
4条回答
  • 2020-12-17 17:57

    I figured it out. It seems I should have been getting events from the ScrollBar (<ListView ScrollBar.Scroll="currentTagNotContactsList_Scroll" in XAML) itself, rather than the viewer. This works, but I just have to figure a way to avoid the event handler being called repeatedly once the scrollbar is down. Maybe a timer would be good:

    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!");
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-17 17:57

    For UWP I got it like this

    <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;
    }
    
    0 讨论(0)
  • 2020-12-17 18:08

    you can try this way:

     <ListView ScrollViewer.ScrollChanged="Scroll_ScrollChanged">
    

    and in Back:

     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!");
        }
    
    0 讨论(0)
  • 2020-12-17 18:14
    //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
                            }
                        }
                    }
    
    0 讨论(0)
提交回复
热议问题