Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 
like image 313
Kilhoffer Avatar asked Apr 29 '10 17:04

Kilhoffer


People also ask

Can user sort columns WPF?

WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns either in ascending or descending order. The sorting can be performed by clicking a column header. You can enable/disable the sorting for all the columns in DataGrid by using DataGrid. AllowSorting property.

What is Datagridview in WPF?

Advertisements. A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −


1 Answers

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

like image 153
Rob Avatar answered Sep 20 '22 15:09

Rob