本文介绍了DataGrid SortDirection被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在启动时指定默认顺序,但仍然允许用户通过单击列标题进行排序。遗憾的是,设置了SortDirection属性后,该属性将被忽略-即,我们获得了正确的列标题箭头,但未对任何内容进行排序。

I want to specify a default ordering at startup, but still allow the user to sort by clicking on the column headers. Sadly the SortDirection property is ignored when it is set - i.e. we get the correct column header arrow, but nothing is sorted.

手动单击标题即可正确地对数据进行排序,因此不是排序本身。这是我正在使用的简化版本:

Clicking on the headers manually, sorts the data correctly, so it's not the sorting itself. This is the simplified version I'm using:

<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中,但效果不佳。这是否与我正在向收藏夹中动态添加新商品有关?即在启动时,列表为空且缓慢填充,也许sortdescription仅应用一次?

Update: I also tried adding SortDescriptions to the ICollectionView as proposed, without good results. Could this have something to do with the fact that I'm dynamically adding new items to the collection? I.e. at startup the list is empty and slowly filled and maybe the sortdescription is only applied once?

推荐答案

看看这个

通过上面的链接:

这篇关于DataGrid SortDirection被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 02:47