c# – DataGrid SortDirection被忽略

我想在启动时指定认排序,但仍允许用户通过点击列标题进行排序.可惜,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中,但没有很好的结果.这可能与我动态地向集合中添加新项目有关吗?即在启动时,列表为空且缓慢填充,也可能仅应用一次排序描述?

解决方法

看看这个 MSDN Blog

从以上链接

DataGridColumn.sortDirection does not actually sort the column. DataGridColumn.sortDirection is used to queue the visual arrow in the DataGridColumnHeader to point up,down,or to not show. To actually sort the columns other than clicking on the DataGridColumnHeader,you can set the DataGrid.Items.sortDescriptions programmatically.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...