加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – DataGrid SortDirection被忽略

发布时间:2020-12-15 04:11:51 所属栏目:百科 来源:网络整理
导读:我想在启动时指定默认排序,但仍允许用户通过点击列标题进行排序.可惜,SortDirection属性被设置时被忽略 – 即我们得到正确的列标题箭头,但没有任何内容被排序. 手动单击标题,正确排序数据,因此它不是排序本身.这是我使用的简化版本: DataGrid ItemsSource="
我想在启动时指定默认排序,但仍允许用户通过点击列标题进行排序.可惜,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读