轉載:http://www.kecq.com/artic-10322303.htmlhtml
之前作了一個數據客戶端,不過是直接連數據庫的,如今認爲這種方式不太好,因而改爲服務端RESTful API+客戶端,數據處理都在服務端。在編寫過程當中遇到一些問題,由於以前沒有采用分頁,因此排序就用DataGrid默認的就行,可是如今須要在服務端分頁了,就發現了一些問題,記錄下來。數據庫
在XAML中有一個名爲dataGrid1的DataGrid,點擊列頭排序只須要在上面加上Sorting屬性後端
<DataGrid Grid.Row="2" x:Name="dataGrid1" AutoGenerateColumns="False" IsReadOnly="True" AlternationCount="2" Sorting="dataGrid1_Sorting" >ui
初始化的時候能夠爲某一列指定排序spa
ICollectionView view = CollectionViewSource.GetDefaultView(list); view.SortDescriptions.Clear(); //先清除全部的排序 SortDescription sd = new SortDescription("列名", ListSortDirection.Descending); view.SortDescriptions.Add(sd);
也能夠在XAML中指定,假設列名是movieNamecode
<DataGrid.Columns>orm
<DataGridTextColumn Binding="{Binding Path=movieName}" SortDirection="Ascending" SortMemberPath="movieName" Header="名稱" />htm
</DataGrid.Columns>blog
下面來看主要排序方法排序
private void dataGrid1_Sorting(object sender, DataGridSortingEventArgs e) { e.Handled = true; //可要可不要 沒去仔細研究 pager.OrderColumn = e.Column.SortMemberPath; ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); pager.Order = "asc"; //默認升序 if (view.SortDescriptions.Count>0) { if(view.SortDescriptions[0].PropertyName== e.Column.SortMemberPath) { if (view.SortDescriptions[0].Direction == System.ComponentModel.ListSortDirection.Ascending) { pager.Order = "desc"; } else if (view.SortDescriptions[0].Direction == ListSortDirection.Descending) { pager.Order = "asc"; } } } LoadData(); }
e.Column.SortMemberPath 爲點擊列頭的列名,問題就在e.Column.SortDirection 這個屬性,若是初始化的時候有指定那麼第一次點擊的時候能獲取到,可是再點擊這個屬性就是null了,沒有指定也是null,好了,我在這裏爲它賦值e.Column.SortDirection=ListSortDirection.Descending 可是下次點擊的時候它仍是爲null 不知爲什麼,網上沒有查到。通過很久摸索,用另外一個方法來
ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
能夠從view.SortDescriptions 中取得有排序的列,判斷它集合大於0而且列名是當前點擊的列,若是當前是升序則變爲降序同時查詢降序的數據,固然最後還得執行清除排序再添加排序
view.SortDescriptions.Clear(); //先清除全部的排序 SortDescription sd = new SortDescription("列名", ListSortDirection.Descending); view.SortDescriptions.Add(sd);
這樣就實現了服務端排序,不知有無更簡單的方法。
補充:
文中提到:我在這裏爲它賦值e.Column.SortDirection=ListSortDirection.Descending,。這裏實際效果從新綁定數據後會丟失之前的排序。應該在綁定數據後再從新綁定列的排序狀態
因此我這裏這樣處理的,
一、後端記錄上次的排序狀態 某列名 升序仍是降序
二、綁定數據後,再次賦值當前的排序狀態。swich記錄的當前排序也就是從新綁定數據前的排序,case某列的排序狀態,而後gridResult2.Columns[2].SortDirection從新設置此列的排序狀態值。這樣就能在DataGridSortingEventArgs e e.Column.SortDirection 屬性中獲取到從新設置的排序值。這樣從新賦值,也能顯示自帶的排序小箭頭。不明白的能夠聯繫www.kaishanguai.com或qq369628105
switch (CurrentOrderByEnum)
{
case (OrderByEnum.OrderByNameAsc):
gridResult2.Columns[2].SortDirection = ListSortDirection.Ascending;
break;
case (OrderByEnum.OrderByNameDesc):
gridResult2.Columns[2].SortDirection = ListSortDirection.Descending;
break;
}
代碼:
<DataGrid x:Name="gridResult2" SelectionUnit="FullRow" MinHeight="420" EnableRowVirtualization ="False" GridLinesVisibility="None" BorderThickness="0" AlternationCount="2" ItemsSource="{Binding}" CanUserAddRows="False" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="False" Visibility="Hidden" IsReadOnly="True" SelectionMode="Extended" Sorting="GridResult2_OnSorting" MouseMove="GridResult2_OnMouseMove" MouseLeftButtonDown="GridResult2_OnMouseLeftButtonDown" MouseLeftButtonUp="GridResult2_OnMouseLeftButtonUp" MouseRightButtonUp="ImageGrid_OnMouseRightButtonDown" LoadingRow="GridResult2_OnLoadingRow" KeyDown="DataView_OnKeyDown" PreviewKeyDown="GridResult2_OnPreviewKeyDown"> <!--SelectionChanged="GridResult2_OnSelectionChanged" --> <!-- This is required to handle CTRL + C when something is selected in the DataGrid --> <DataGrid.CommandBindings> <CommandBinding Command="Copy" Executed="CopyCommand" /> </DataGrid.CommandBindings> <!-- This is required to handle CTRL + C when something is selected in the DataGrid --> <DataGrid.InputBindings> <KeyBinding Key="C" Modifiers="Control" Command="Copy" /> </DataGrid.InputBindings> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox Name="ChkAll" ToolTip="All" Content="All" IsChecked="False" Unchecked="UnAll_Checked" Checked="All_Checked"></CheckBox> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Checked="ItemFile_OnChecked" Unchecked="ItemFile_OnUnchecked" Tag="{Binding FilePath}" HorizontalAlignment="Center" x:Name="itemFile" VerticalAlignment="Center" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header=""> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image Source="{Binding SnapshotPath}" Height="20" Width="20" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Name" Binding="{Binding FileName}" /> <DataGridTemplateColumn Header="MD5"> <DataGridTemplateColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"></Setter> </Style> </DataGridTemplateColumn.HeaderStyle> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" Text="{Binding FileMD5}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Modified Date" CanUserSort="True" SortMemberPath="FileModifiedTime"> <DataGridTemplateColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"></Setter> </Style> </DataGridTemplateColumn.HeaderStyle> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" Text="{Binding FileModifiedTime}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Path" Binding="{Binding FilePathNoName}" /> <DataGridTemplateColumn Header="Classify Type" CanUserSort="True" SortMemberPath="ClassifyType"> <DataGridTemplateColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"></Setter> </Style> </DataGridTemplateColumn.HeaderStyle> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" Text="{Binding ClassifyType}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Type"> <DataGridTemplateColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"></Setter> </Style> </DataGridTemplateColumn.HeaderStyle> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" Text="{Binding FileExtension}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Size(KB)" CanUserSort="True" SortMemberPath="FileSize"> <DataGridTemplateColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"></Setter> </Style> </DataGridTemplateColumn.HeaderStyle> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Center" Text="{Binding FileSize, StringFormat={}{0:N0}}"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
//綁定數據後,從新設置排序狀態
gridResult2.ItemsSource = bindDataList; //設置排序圖標 if (IsClickSort) { switch (CurrentOrderByEnum) { case (OrderByEnum.OrderByNameAsc): gridResult2.Columns[2].SortDirection = ListSortDirection.Ascending ; break; case (OrderByEnum.OrderByNameDesc): gridResult2.Columns[2].SortDirection = ListSortDirection.Descending; break; case (OrderByEnum.OrderByModifiedDateAsc): gridResult2.Columns[4].SortDirection = ListSortDirection.Ascending ; break; case (OrderByEnum.OrderByModifiedDateDesc): gridResult2.Columns[4].SortDirection = ListSortDirection.Descending; break; case (OrderByEnum.OrderByClassifyAsc): gridResult2.Columns[6].SortDirection = ListSortDirection.Ascending; break; case (OrderByEnum.OrderByClassifyDesc): gridResult2.Columns[6].SortDirection = ListSortDirection.Descending ; break; case (OrderByEnum.OrderBySizeAsc): gridResult2.Columns[8].SortDirection = ListSortDirection.Ascending; break; case (OrderByEnum.OrderBySizeDesc): gridResult2.Columns[8].SortDirection = ListSortDirection.Descending; break; } } else { gridResult2.Columns[4].SortDirection = ListSortDirection.Descending; }
//排序事件
private void GridResult2_OnSorting(object sender, DataGridSortingEventArgs e) { ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending; bool sortAscending = direction == ListSortDirection.Ascending; switch (e.Column.SortMemberPath) { case ("FileName"): if (sortAscending) { CurrentOrderByEnum = OrderByEnum.OrderByNameAsc; } else { CurrentOrderByEnum = OrderByEnum.OrderByNameDesc; } break; case ("FileModifiedTime"): if (sortAscending) { CurrentOrderByEnum = OrderByEnum.OrderByModifiedDateAsc; } else { CurrentOrderByEnum = OrderByEnum.OrderByModifiedDateDesc; } break; case ("ClassifyType"): if (sortAscending) { CurrentOrderByEnum = OrderByEnum.OrderByClassifyAsc; } else { CurrentOrderByEnum = OrderByEnum.OrderByClassifyDesc; } break; case ("FileSize"): if (sortAscending) { CurrentOrderByEnum = OrderByEnum.OrderBySizeAsc; } else { CurrentOrderByEnum = OrderByEnum.OrderBySizeDesc; } break; } IsClickSort = true; Search(); }