【发布时间】:2013-05-07 09:02:26
【问题描述】:
我有一个 WPF DataGrid 绑定到要在 DataGrid 上显示的约会对象的 ICollectionView。我的约会都有一个 DateTime 字段,我试图按日期分组并按时间排序。我尝试了各种配置和日期时间格式,虽然分组正确完成,但组本身并没有排序。
添加新约会会将约会日期时间默认为现在。当我更改日期时,如果该日期不存在,则会添加一个新组,但不会对这些组进行排序。
我使用了在这里找到的 GroupStyle:http://www.wpftutorial.net/DataGrid.html#grouping
我还在为 DataGrid 列使用扩展 WPF 工具包(通过 nuget 下载)DateTimePicker:
<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));
任何帮助\建议将不胜感激。谢谢
【问题讨论】: