视图分组排序
效果:
实现步骤:
第一步:为分组做一个标题头,就是效果图中的浅蓝色部分:
<DataGrid.GroupStyle>标签部分:
<DataGrid x:Name="dgDataPiontInformation" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Name}" Foreground="White" FontWeight="Bold" Background="LightSkyBlue" HorizontalAlignment="Left" Margin="0,5,0,0" MaxWidth="100" Height="30"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </DataGrid.GroupStyle> <DataGrid.Columns> 。。。。。 </DataGrid.Columns> </DataGrid>
特别提醒:
Text="{Binding Path=Name}"
指的是绑定PropertyGroupDescription的Name属性,而不是绑定的数据对象中的属性。
第二步:cs代码:
this.dgDataPiontInformation.ItemsSource = getAllDataPointsInfoResponse.DataPointInfoViews;
//排序
ICollectionView sortView = CollectionViewSource.GetDefaultView(this.dgDataPiontInformation.ItemsSource);
sortView.SortDescriptions.Add(new SortDescription("Number", ListSortDirection.Descending));
//分组
ICollectionView view = CollectionViewSource.GetDefaultView(this.dgDataPiontInformation.ItemsSource);
view.GroupDescriptions.Add(new PropertyGroupDescription("Number"));
【The End】