且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 如何判断ListBox控件滚动到底

更新时间:2022-10-03 17:43:37

假如ListBox控件绑定的数据很大的时候,通常会造成加载的速度很慢,那么有一种交互方案可以优化一下这种情况,就是先在ListBox上加载一部分的数据,等到用户查看的时候将ListBox滚动到底的时候再加载一部分数据。但是在ListBox控件里面根本就没有相关的事件和属性来判断出来ListBox什么时候滚动到底了,那么下面讲解一种解决的方法。

ListBox控件其实是封装了ScrollViewer控件和ScrollBar控件在里面的。那这就好办了,通过获取ListBox控件里面封装的ScrollViewer控件,然后通过ScrollViewer控件的属性就可以判断出来ListBox控件是否滚动到底了。
下面看一下代码:

 

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            
<ListBox Name="listbox1" MouseMove="listbox1_MouseMove" >
            
</ListBox>
        
</Grid> 

 

 


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Input;  
  6. using System.Windows.Media;  
  7. using Microsoft.Phone.Controls;  
  8.  
  9. namespace ListBoxUpdate  
  10. {  
  11.     public partial class MainPage : PhoneApplicationPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.  
  17.             for (int i = 0; i < 30; i++)  
  18.             {  
  19.                 listbox1.Items.Add("项目"+i);  
  20.             }  
  21.               
  22.         }  
  23.  
  24.         private void listbox1_MouseMove(object sender, MouseEventArgs e)  
  25.         {  
  26.             //获取listbox的子类型ScrollViewer  
  27.             ScrollViewer scrollViewer = FindChildOfType<ScrollViewer>(listbox1);//ScrollViewer  scrollBar  
  28.             if (scrollViewer == null)  
  29.             {  
  30.                 throw new InvalidOperationException("erro");  
  31.             }  
  32.             else  
  33.             {  
  34.                 //判断当前滚动的高度是否大于或者等于scrollViewer实际可滚动高度,如果等于或者大于就证明到底了  
  35.                 if (scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight)  
  36.                 {  
  37.                     //处理listbox滚动到底的事情  
  38.                     for (int i = 0; i < 10; i++)  
  39.                     {  
  40.                         int k = listbox1.Items.Count;  
  41.                         listbox1.Items.Add("项目" + k);  
  42.                         k++;  
  43.                     }  
  44.                 }  
  45.             }  
  46.         }  
  47.  
  48.         //获取子类型  
  49.         static T FindChildOfType<T>(DependencyObject root) where T : class  
  50.         {  
  51.             var queue = new Queue<DependencyObject>();  
  52.             queue.Enqueue(root);  
  53.  
  54.             while (queue.Count > 0)  
  55.             {  
  56.                 DependencyObject current = queue.Dequeue();  
  57.                 for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)  
  58.                 {  
  59.                     var child = VisualTreeHelper.GetChild(current, i);  
  60.                     var typedChild = child as T;  
  61.                     if (typedChild != null)  
  62.                     {  
  63.                         return typedChild;  
  64.                     }  
  65.                     queue.Enqueue(child);  
  66.                 }  
  67.             }  
  68.             return null;  
  69.         }  
  70.     }  

运行的效果

 

Windows Phone 7 如何判断ListBox控件滚动到底 

不知道大家有没有更加好的解决方法???
 
下面有高人建议使用一个扩展过的Listbox控件LazyListBox,查到的博文链接:
http://blogs.msdn.com/b/ptorr/archive/2010/10/12/procrastination-ftw-lazylistbox-should-improve-your-scrolling-performance-and-responsiveness.aspx
 
 

 本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078562