WPF4 Datagrid doesn't sort on column headers

Viewed 14464

I am trying to implement my first WPF application using an MVVM design pattern.

I created an application that is databinding to an ObservableCollection<>. The application renders fine, but I expect the datagrid to re-sort the rows when I click on the column headers.

I've researched posts that said: The data source has to implement IEnumerable, mine does. I need to set CanUserSortColumns and/or CanUserSort (on each column), I did. I could implement a custom sort by specifying the Sorting callback function, I did.

None of these seem to have helped. In the debugger, my _customerGrid_Sorting function never gets called and clicking on the column headers has no effect.

Am I missing something in the XAML that follows? Is there another interface I need to implement? I am feeling very lost right now, so any help is appreciated.

XAML:

        <DataGrid Name="_customerGrid" 
            AutoGenerateColumns="False" 
            CanUserSortColumns="True"
            ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
            HorizontalAlignment="Left" 
            ItemsSource="{Binding Path=AllCustomers}" 
            RowDetailsVisibilityMode="VisibleWhenSelected"
            RowStyle="{StaticResource DataGridRowStyle}"
            SelectionUnit="FullRow"
            Sorting="_customerGrid_Sorting"
            VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Cust ID" MinWidth="90" CanUserSort="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=CustID}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Name" MinWidth="300" CanUserSort="True" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
2 Answers
Related