我想在启动时指定默认排序,但仍然允许用户通过单击列标题进行排序。遗憾的是,SortDirection 属性在设置时被忽略 - 即我们得到正确的列标题箭头,但没有排序。
手动单击标题,对数据进行正确排序,因此不是排序本身。这是我正在使用的简化版本:
<DataGrid ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=CurrentView}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Header 1" Binding="{Binding ApplicationName}"/>
<DataGridTextColumn Header="Header 2"
Binding="{Binding TotalTime}" SortDirection="Descending"/>
</DataGrid.Columns>
</DataGrid>
更新:我还尝试按照建议将 SortDescriptions 添加到 ICollectionView,但效果不佳。这可能与我正在向集合中动态添加新项目的事实有关吗?即在启动时列表是空的并慢慢填充,也许排序描述只应用一次?