2009-08-19 122 views
9

有沒有辦法檢測ListViewScrollViewer的滾動條是否已經到達虛擬滾動空間的底部?我想檢測到這一點,從服務器獲取更多項目,將其放入的ListView檢測WPF 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!"); 
    } 

} 

這甚至是否正確?我還需要區分引起事件的垂直滾動條和引起滾動條的水平滾動條(即,如果您在框的底部水平滾動,我不想繼續生成對服務器的調用)。

謝謝。

回答

8

我想通了。看來我應該從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!"); 

    } 

} 
+2

ScrollBar.Scroll的ListView控件在Windows 10中不存在..如何實現這一要求窗戶10 – djkp 2015-07-08 11:48:05

1
//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

對於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; 
}