I am trying to get a WPF DataGrid to sort by a column by default and it isn't working. there is no sorting happening. The sort icons appear and if i click them then sorting happens.
Right now my table has just the one column. but is it still not sorting it by default.
Any ideas what i am missing?
<DataGrid.Columns>
<!--Ordinal-->
<DataGridTemplateColumn d:DataContext="{d:DesignInstance tabViewModels:ColumnViewModel}"
SortMemberPath="Ordinal"
>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label Content="#" ToolTip="Column Position" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<xctk:DoubleUpDown Value="{Binding Ordinal, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Ordinal}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
One solution would be to use a LINQ query to give the data already sorted to the DataGrid. Modify the getter of the Item Source with something like this:
List<Something> MyItemSource
{
get
{
return _myItemSource.OrderBy(x => x.Ordinal).ToList();
}
}
then it will for sure sort by Ordinal.