我有一個非常基本的DataGrid
與一些測試數據。數據提供爲ObservableCollection<T>
的Client
objects
,其包含Organization
,FirstName
和LastName
特性。我通過Organization
對項目進行分組,並且希望能夠通過FirstName
和LastName
在特定組內排序(此部分功能正常),並按Organization
排序組。最後一部分是我遇到的問題。Sorting DataGrid groups
如果我不指定任何排序屬性,或者如果我指定CollectionViewSource.SortDescriptions
屬性(見下文),如PropertyName="Name"
或PropertyName="CompleteNonsense"
,除了PropertyName="Organization"
,這將讓我做一個初步的排序。如果您查看下面的圖片,公司名稱會亂序顯示,因爲這是集合初始化的方式。如果我點擊Organization
標題,它會按升序對它進行排序,但不會讓我在此之後再進行排序。如果我再次點擊標題,它會改變箭頭,它表示排序的方向,但不會發生實際的排序。無論我指定什麼排序屬性,即使是完全任意的屬性,除非它是PropertyName="Organization"
,或者沒有指定屬性,否則這是完全相同的。
如果我在PropertyName="Organization"
上指定排序,它將從升序開始排序,但如果單擊標題則不排序。所以,看起來該屬性名稱確實會觸發排序,但只是第一次。
截圖:
這裏的XAML
爲DataGrid
:
<DataGrid ItemsSource="{Binding Source={StaticResource GroupedData}}"
AutoGenerateColumns="False" IsReadOnly="True"
GridLinesVisibility="None" HeadersVisibility="Column"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="False" Background="White">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Margin" Value="10,0,0,0"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<DockPanel Background="LightBlue" DataContext="{Binding Items}">
<TextBlock Text="{Binding Path=Organization}"
Foreground="Blue" Margin="5,0,0,0" Width="100"/>
<TextBlock Text="Employee Count:" Foreground="Blue" Margin="40,0,0,0"/>
<TextBlock Text="{Binding Path=Count}" Foreground="Blue"
Margin="5,0,0,0"/>
</DockPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Organization" Binding="{Binding Organization}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Width="*"/>
</DataGrid.Columns>
</DataGrid>
而這裏的XAML
的Grouping
和Sorting
:
<CollectionViewSource Source="{Binding Data}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Organization"/>
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Organization"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
有沒有人知道我可以通過Organization
超出最初的排序組?
編輯:
我一直用這個亂搞(真的需要弄清楚了這一點),並創建了頭Click
事件的快速代碼隱藏事件處理程序:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="ColumnHeader_Click"/>
</Style>
</DataGrid.ColumnHeaderStyle>
private void ColumnHeader_Click(object sender, RoutedEventArgs e)
{
var columnHeader = sender as DataGridColumnHeader;
if (columnHeader != null && columnHeader.Content.ToString().Equals("Organization"))
{
if (columnHeader.Column.SortDirection == ListSortDirection.Descending)
{
columnHeader.Column.SortDirection = ListSortDirection.Ascending;
MessageBox.Show("Ascending");
}
else
{
columnHeader.Column.SortDirection = ListSortDirection.Descending;
MessageBox.Show("Descending");
}
}
}
上Organization
每次點擊後,"Descending"
彈出,將箭頭向下排序,點擊OK
後立即恢復向上。這些組的實際排序不會在視覺上發生。有些東西讓它卡在Ascending
訂單。
如果我已經兩組樣'組織'和'部門'?並對'Department'進行排序。 – nag 2017-01-17 11:00:57