问题:在使用ListBox时,我们有时想要listbox滚动到页面底部时立即显示下一部分的内容
解决办法:ListBox控件并没有一个事件可以监控其Scroll当前的滑动情况,但ListBox含有ScrollBar子控件,如果我们可以监控其ScrollBar子控件是否移动到了底部,那我们就能监控ListBox是否滚动到了底部
示例:
应用启动时先为ListBox添加50个Item,当Scroll滑动到底部时,自动再添加50个Item
实现:
目录:
1) 创建一个基本应用并添加一个ListBox事件
2) 为ListBox添加事件代码
3) 测试运行
1 创建一个基本应用并添加一个ListBox控件
1) 创建一个基本的Windows Phone应用程序,OS版本7.0
2) 添加ListBox
1 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 2 <ListBox Name="listBox1"/> 3 </Grid>
2 为ListBox添加事件代码
1) 在PhoneApplicationPage_Loaded事件中为listBox1添加50个Item
1 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 2 { 3 for (int i = 0; i < 50; i++) 4 { 5 listBox1.Items.Add(i.ToString()); 6 }
2) 接着找出listBox1中所有的ScrollBar子控
1 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 2 { 3 for (int i = 0; i < 50; i++) 4 { 5 listBox1.Items.Add(i.ToString()); 6 } 7 //找到listBox1中属于ScrollBar的子控件 8 List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(listBox1);
其中函数GetVisualChildCollection:
1 //返回parent控件中属于ScrollBar的子控件 2 public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement 3 { 4 List<T> visualCollection = new List<T>(); 5 //查找parent控件中所有ScrollBar子控件,并通过visualCollection返回 6 GetVisualChildCollection(parent as DependencyObject, visualCollection); 7 return visualCollection; 8 }
1 //查找parent控件中所有ScrollBar子控件,并通过visualCollection返回 2 private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement 3 { 4 //查找parent的子控件集合中存在的子级数目 5 int count = VisualTreeHelper.GetChildrenCount(parent); 6 for (int i = 0; i < count; i++) 7 { 8 //获取parent控件的i级子控件 9 DependencyObject child = VisualTreeHelper.GetChild(parent, i); 10 if (child is T) 11 { 12 //若子控件属于ScrollBar控件则将之添加到结果List中 13 visualCollection.Add(child as T); 14 } 15 else if (child != null) 16 { 17 //若子控件不属于ScrollBar控件,且不是null,则使用递归方法再次查询该子控件的子控件 18 GetVisualChildCollection(child, visualCollection); 19 } 20 } 21 }
3) 遍历listBox1的所有ScrollBar子控件,并为其绑定ValueChanged事件
1 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 2 { 3 for (int i = 0; i < 50; i++) 4 { 5 listBox1.Items.Add(i.ToString()); 6 } 7 //找到listBox1中属于ScrollBar的子控件 8 List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(listBox1); 9 //遍历scrollBarList 10 foreach (ScrollBar scrollBar in scrollBarList) 11 { 12 if (scrollBar.Orientation == System.Windows.Controls.Orientation.Horizontal) 13 { 14 //该ScrollBar是水平显示的 15 } 16 else 17 { 18 //绑定该子控件的ValueChanged事件 19 scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar_ValueChanged); 20 } 21 } 22 }
其中verticalScrollBar_ValueChanged函数:
1 //ScrollBar的ValueChanged事件 2 private void verticalScrollBar_ValueChanged(object sender, RoutedEventArgs e) 3 { 4 ScrollBar scrollBar = (ScrollBar)sender; 5 //读取ScrollBar的当前值与最大值 6 object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty); 7 object maxObj = scrollBar.GetValue(ScrollBar.MaximumProperty); 8 if (valueObj != null && maxObj != null) 9 { 10 double value = (double)valueObj; 11 double max = (double)maxObj; 12 //当前值接近最大值时 即快滑到底部时 13 if (value >= max) 14 { 15 for (int i = 0; i < 50; i++) 16 { 17 listBox1.Items.Add(i.ToString()); 18 } 19 } 20 } 21 }
3 测试运行
说明:
1) 这个办法是从网上找来的,我只是对其做了一些注释便于理解。
2) 这个方法其实不太好使,用户体验的感觉没有新浪微博的Android客户端那么流畅,希望有牛人能改进一下