C# 如何在清除排序描述后删除wpf网格排序箭头

C# 如何在清除排序描述后删除wpf网格排序箭头,c#,wpf,sorting,c#-4.0,wpfdatagrid,C#,Wpf,Sorting,C# 4.0,Wpfdatagrid,我单击网格标题对列进行排序,然后单击“重置”按钮通过其集合视图清除排序描述。但排序箭头图标仍保留在标题中。如何移除它?我能想到的简单解决方案是 foreach (DataGridColumn column in DataGridView.Columns) { column.SortDirection = null; } 我在试图解决如何从网格中完全清除排序时遇到了这个问题。感谢[krishnaaditya]回答了如何清除标题中的排序箭头 using System.Windows.Dat

我单击网格标题对列进行排序,然后单击“重置”按钮通过其集合视图清除排序描述。但排序箭头图标仍保留在标题中。如何移除它?

我能想到的简单解决方案是

foreach (DataGridColumn column in DataGridView.Columns)
{
    column.SortDirection = null;
}

我在试图解决如何从网格中完全清除排序时遇到了这个问题。感谢[krishnaaditya]回答了如何清除标题中的排序箭头

using System.Windows.Data;
using System.ComponentModel;

ICollectionView view = CollectionViewSource.GetDefaultView(resultsGrid.ItemsSource);
if (view != null && view.SortDescriptions.Count > 0)
{
    view.SortDescriptions.Clear();
    foreach (DataGridColumn column in resultsGrid.Columns)
    {
        column.SortDirection = null;
    }
}

这是完整的解决方案。第一个答案只删除了排序,但没有删除标题中的箭头。不幸的是,如果对使用转换器显示良好数据的列进行排序(例如,在一个列中有一个枚举,而使用转换器显示其他内容),则上述解决方案均无效。在这种情况下,SortDescriptions.Count始终保持为0。当您尝试重置排序时,什么也没有发生。这是因为您在这里搞乱了源,而不是实际显示的值。请尝试DataGrid.Items.SortDescriptions。这就是实际值所在的位置。将其包装在带有延迟刷新的using中,以获得最佳性能。