How can I sort a DataGridTemplateColumn on a WPF Toolkit DataGrid?

Kilhoffer picture Kilhoffer · Apr 29, 2010 · Viewed 34.3k times · Source

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>

Answer

Rob picture Rob · Apr 29, 2010

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