How can I sort a DataGridTemplateColumn on a WPF Toolkit DataGrid?
Asked Answered
S

3

107

I have a WPF Toolkit DataGrid with one DataGridTemplateColumn. I've specified in a grid attribute that I wish all columns to be sortable, but the DataGridTemplateColumn won't allow it. All other columns do allow sorting. I've even tried explicitly setting CanUserSort to true for that column, but no luck. Is it even possible to sort a template column? I've provided a custom sorter that works for all columns, but the header won't allow a sort click.

<Controls:DataGrid ItemsSource="{Binding Events}" AutoGenerateColumns="False" 
                               CanUserSortColumns="True" 
                               CanUserReorderColumns="False" 
                               Sorting="DataGrid_Sorting" 
                               x:Name="EventsGrid">
                <Controls:DataGrid.Columns>
                    <Controls:DataGridTemplateColumn Header="Type" Width="42" CanUserResize="False">
                        <Controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding EventTypeImage, Mode=OneWay}" HorizontalAlignment="Center" Width="16"/>
                            </DataTemplate>
                        </Controls:DataGridTemplateColumn.CellTemplate>
                    </Controls:DataGridTemplateColumn>
                    <Controls:DataGridTextColumn Header="Source" 
                                           Binding="{Binding Source, Mode=OneWay}" />
                    <Controls:DataGridTextColumn Header="Details" MinWidth="175" 
                                           Binding="{Binding Details, Mode=OneWay}" />
                    <Controls:DataGridTextColumn Header="Timestamp" MinWidth="175"  
                                           Binding="{Binding Timestamp, Mode=OneWay}" 
                                           IsReadOnly="True"/>
                </Controls:DataGrid.Columns>
            </Controls:DataGrid>
Squire answered 29/4, 2010 at 17:13 Comment(0)
M
190

You need to set the SortMemberPath property on Controls:DataGridTemplateColumn to the name of the CLR property you want to sort on.

Mauser answered 29/4, 2010 at 17:35 Comment(3)
One word of warning. This will not automatically re-sort if you change the underlying data.Epigraph
Another note: this may sound silly, but if your column does not have a Header (the title) it will also not work - clicking the header triggers the sorting. I have a column that did not need a header and I sorted this out by setting Header=" "Nebiim
Working as expectedWanting
V
42

Added SortMemberPath="Name" to DataGridTemplateColumn. It started sorting.

<data:DataGridTemplateColumn Header="Name" SortMemberPath="Name" CanUserSort="True">
Venusberg answered 30/4, 2010 at 1:29 Comment(0)
H
16
 <DataGridTemplateColumn SortMemberPath="DataDiNascita" Header="Data di nascita" IsReadOnly="True">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding Path=DataDiNascita}"></DatePicker>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataDiNascita,StringFormat=\{0:dd/MM/yyyy\}}"></TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
Harlandharle answered 27/3, 2014 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.