1

I've have a WPF DataGrid bound to an ICollectionView of appointment objects to be displayed on the DataGrid. My appointments each have a DateTime field and I'm trying to group by the date and sort by the time. 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

I am also using the Extended WPF Toolkit (downloaded through nuget) DateTimePicker for the DataGrid Column:

<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:

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

4

1 回答 1

1

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. my grid sorts correctly now after i update.

于 2013-06-05T11:58:45.877 回答