简体   繁体   English

WPF DataGrid按日期和时间分组和排序

[英]WPF DataGrid Grouping and Sorting by Date and Time

I've have a WPF DataGrid bound to an ICollectionView of appointment objects to be displayed on the DataGrid. 我有一个WPF DataGrid绑定到要显示在DataGrid上的约会对象的ICollectionView上。 My appointments each have a DateTime field and I'm trying to group by the date and sort by the time. 我的约会每个都有一个DateTime字段,我试图按日期分组并按时间排序。 I've tried various configurations and date time formats, and although the grouping is done correctly, the groups themselves are not sorting. 我尝试了各种配置和日期时间格式,尽管分组正确完成,但分组本身并未排序。

Adding a new Appointment defaults the appointment datetime to Now. 添加新的约会默认将约会日期时间设置为现在。 When I change the date, a new group for that date is added if it does not exists, but the groups are not sorted. 当我更改日期时,如果该日期不存在,则会添加该日期的新组,但不会对这些组进行排序。

I used a GroupStyle found here: http://www.wpftutorial.net/DataGrid.html#grouping 我使用了在此处找到的GroupStyle: http : //www.wpftutorial.net/DataGrid.html#grouping

I am also using the Extended WPF Toolkit (downloaded through nuget) DateTimePicker for the DataGrid Column: 我还将DataGrid列使用Extended WPF Toolkit(通过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>

My Code for the ICollectionView initialisation is this: 我为ICollectionView初始化的代码是这样的:

AppointmentList = CollectionViewSource.GetDefaultView(GetAppointments());

AppointmentList.GroupDescriptions.Add(new PropertyGroupDescription("Appointment.AppointmentDateTime.Date"));

AppointmentList.SortDescriptions.Add(new SortDescription("Appointment.AppointmentDateTime", ListSortDirection.Ascending));

Any help\\advice would be greatly appreciated. 任何帮助\\建议将不胜感激。 Thanks 谢谢

I ended up making all columns of the datagrid readonly, and adding a few textboxes with which I can edit the selected item of the data grid. 最后,我将datagrid的所有列都设为只读,并添加了一些文本框,可用来编辑数据网格的选定项。 my grid sorts correctly now after i update. 更新后,我的网格现在可以正确排序了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM