1
我有一个WPF DataGrid绑定到DataGrid上要显示的约会对象的ICollectionView。我的约会每个都有一个日期时间字段,我试图按日期分组,并按时间排序。我尝试了各种配置和日期时间格式,虽然分组是正确完成的,但这些组本身并未进行排序。WPF DataGrid按日期和时间分组和排序
添加新的约会会将约会的日期时间默认为“现在”。当我更改日期时,如果该日期不存在,则会添加该日期的新组,但这些组未被排序。
我用GroupStyle在这里找到:http://www.wpftutorial.net/DataGrid.html#grouping
我也使用扩展WPF工具包(通过的NuGet下载)的DateTimePicker的DataGrid列:
<DataGridTemplateColumn
x:Name="AppointmentDateTimeColumn"
Header="Time"
Width="Auto">
<DataGridTemplateColumn.CellStyle>
<Style
TargetType="{x:Type DataGridCell}">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type DataGridCell}">
<Grid
Background="{TemplateBinding Background}">
<ContentPresenter
VerticalAlignment="Stretch" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xctk:DateTimePicker
Value="{Binding Path=Appointment.AppointmentDateTime, Mode=TwoWay}"
Format="ShortTime" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<xctk:DateTimePicker
Value="{Binding Path=Appointment.AppointmentDateTime, Mode=TwoWay}"
Format="ShortTime" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
我对的ICollectionView的初始化代码是这样的:
AppointmentList = CollectionViewSource.GetDefaultView(GetAppointments());
AppointmentList.GroupDescriptions.Add(new PropertyGroupDescription("Appointment.AppointmentDateTime.Date"));
AppointmentList.SortDescriptions.Add(new SortDescription("Appointment.AppointmentDateTime", ListSortDirection.Ascending));
任何帮助\的建议将不胜感激。谢谢