WPF的Datagrid:清除列排序 [英] WPF Datagrid: Clear column sorting

查看:2862
本文介绍了WPF的Datagrid:清除列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序,其中列可以通过点击标题进行排序使用WPF的Datagrid。

I am using a WPF Datagrid in my application where columns can be sorted by clicking on the header.

我想知道是否有任何的方法来清除列的编程排序?

I was wondering if there was any way to clear a column's sorting programatically ?

我试图整理一列,然后清除 MyDataGrid.Items.SortDescriptions ,但该集合是空的(即使一列进行了排序)

I tried sorting a column and then clearing MyDataGrid.Items.SortDescriptions, but that collection was empty (even though one column was sorted).

我也试过:

MyDataGridColumn.SortDirection = null;

的问题是,该列指示走了,但排序编辑的单元格,切换时仍然发生。行

The problem is that the column indication is gone, but the sorting still occurs when editing a cell and switching rows.

请问有没有办法来清除列的排序

Is there no way to clear a column's sort ?

Edit(对于清晰度):这个问题是,我想再次允许排序如果在同一列标题用户再次点击,所以设置CanUserSort为false将是有问题的,即使是在XAML中完成的。简而言之,我试图做的,是防止一旦排序列被修饰的细胞被命令行。我想强迫用户在报头重新按

Edit (for clarity): The problem is that I'd like to allow sorting again if the user re-clicks on the same column header, so setting CanUserSort to false would be problematic, even if it were done in the XAML. In short, what I'm attempting to do, is prevent rows from being ordered once a sorted column has a cell that was modified. I want to force the user to re-click on the header.

推荐答案

下面是你需要什么:

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

        ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
        if (view != null && view.SortDescriptions != null)
        {
            view.SortDescriptions.Clear();
            foreach (var column in grid.Columns)
            {
                column.SortDirection = null;
            }
        }



原始来源:的http://stackoverflow.com/a/9533076/964053

我想知道的是什么什么M $想不把一个ClearSort()方法...

What I want to know is what was M$ thinking for not putting a ClearSort() method...

这篇关于WPF的Datagrid:清除列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆