【问题标题】:WPF DataGrid Grouping and Sorting by Date and TimeWPF DataGrid 按日期和时间分组和排序
【发布时间】: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));

任何帮助\建议将不胜感激。谢谢

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我最终将数据网格的所有列设为只读,并添加了一些文本框,我可以使用这些文本框编辑数据网格的选定项目。更新后,我的网格现在可以正确排序了。

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多