微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/csharp/92523.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐