XAML:
<GridView x:Name="itemGridView" > <GridView.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </GridView.ItemsPanel> <GridView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <Grid Margin="1,0,0,6"> <Button AutomationProperties.Name="Group Title" Click="Header_Click" Style="{StaticResource TextPrimaryButtonStyle}" > <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" /> <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/> </StackPanel> </Button> </Grid> </DataTemplate> </GroupStyle.HeaderTemplate> <GroupStyle.Panel> <ItemsPanelTemplate> <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </GridView.GroupStyle> </GridView>
.cs:
/// <summary> /// 根据名称找到控件的内容控件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parent"></param> /// <param name="name"></param> /// <returns></returns> public T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(Control.NameProperty) as string; if (controlName == name) { return child as T; } else { T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } } return null; }
XAML-.cs:
//找到gridview中的 scrollViewer 、 scrollBar 注册值改变事件 //Tip 获取该事件要在绑定数据后才去注册,不然获取不到scrollviewer var scrollViewer = this.FindVisualChildByName<ScrollViewer>(this.itemGridView, "ScrollViewer");
//找到水平的滚轴 var scroollBar = this.FindVisualChildByName<ScrollBar>(scrollViewer, "HorizontalScrollBar"); scroollBar.ValueChanged += scroollBar_ValueChanged;
private void scroollBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { ScrollBar scrollbar = sender as ScrollBar; //当滚轴值为最大时并且没有在加载数据时 if (e.NewValue == scrollbar.Maximum && !isLoading) { //异步加载数据 LoadData(); //设置滚轴的值偏移点,以便再次出发valueChanged事件 scrollbar.Value = e.NewValue - 1E-8; } if (e.NewValue == scrollbar.Minimum) { } }