我正在尝试使用MVVM设计模式实现我的第一个WPF应用程序。
我创建了一个绑定到ObservableCollection<>的应用程序。应用程序渲染正常,但我希望当我点击列标题时,数据表格能够重新排序行。
我研究了一些帖子,其中提到: 数据源必须实现IEnumerable,我的确实现了。 我需要设置CanUserSortColumns和/或CanUserSort(在每个列上),我已经设置了。 我可以通过指定Sorting回调函数来实现自定义排序,我也这样做了。
但是这些似乎都没有起作用。在调试器中,我的_customerGrid_Sorting函数从未被调用,点击列标题也没有任何效果。
我是否在下面的XAML代码中漏掉了什么?还有其他的接口我需要实现吗?我感到非常迷茫,所以非常感谢任何帮助。
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>