即使在排序之后,如何将DataGrid的一行或多行始终保持在顶部

时间:2015-06-11 20:50:56

标签: c# wpf sorting datagrid

我有一个DataGrid(在视图上)绑定到一个ObservableCollection(在ViewModel上)。

对于这个ObservableCollection,我正在以编程方式添加一个"魔术行",因为所选项目将用于在其他地方对另一个集合进行排序,我将这个虚拟行添加到" clear"过滤。

目前,当View加载时,我的魔术字符串总是出现在顶部(因为我将其插入索引0),但是当我单击标题以更改排序顺序时,行按字母顺序重新排列我的魔法行在瓦砾中消失了。

我想要的是,即使我点击标题更改排序顺序,该魔术行也始终显示在顶部。

视图模型:

// (...)

public ObservableCollection<FilteringItem> ItemTypes
{
    get
    {
        var result = new ObservableCollection<FilteringItem>(_setups.Select(n => n.ItemType)
                                                             .Distinct()
                                                             .Select(s => new FilteringItem(s, s))
                                                             .OrderBy(v => v.Name));


        // Magic Happens Here: adding a "magic" row to remove filtering
        // (that is, "allow all") when used by some filtering method elsewhere;
        string allString = String.Format("All ({0})", result.Count);
        var filteringAll = new FilteringItem(allString, "");

        result.Insert(0, filteringAll);

        return result;
    }

}
// (...)

public class FilteringItem
{
    public string Name { get; private set; }
    public string Value { get; private set; }

    public FilteringItem(string name, string val)
    {
        Name = name;
        Value = val;
    }
}    

查看:

<DataGrid ItemsSource="{Binding ItemTypes}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Tipo" Width="*" Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>

1 个答案:

答案 0 :(得分:1)

感谢this blog post,我已经开始工作了。

“秘密”是截取Sorting事件(将e.Handled设置为true),然后将DataGrid.ItemsSource转换为ListCollectionView,然后分配自定义IComparerListCollectionView.CustomSort财产。

然后,在排序时,你以某种方式识别你的固定行(在我的情况下是空值),并使它总是到达顶部(这又取决于ListSortDirection)。

public partial class ViewContainingDataGrid : UserControl
{
    public ViewContainingDataGrid()
    {
        this.InitializeComponent();
    }

    private void datagrid_Sorting(object sender, DataGridSortingEventArgs e)
    {
        e.Handled = true;
        SmartSort(sender, e.Column);
    }

    private void SmartSort(object sender, DataGridColumn column)
    {
        ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ?
                             ListSortDirection.Ascending : ListSortDirection.Descending;

        column.SortDirection = direction;
        var dg = sender as DataGrid;
        var lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(dg.ItemsSource);
        lcv.CustomSort = new SmartSorter(direction);
    }

}

public class SmartSorter : IComparer
{
    public ListSortDirection Direction { get; private set; }

    public SmartSorter(ListSortDirection direction)
    {
        Direction = direction;
    }


    public int Compare(object x, object y)
    {
        string valorx = x as string;
        string valory = y as string;

        int comparison = valorx.CompareTo(valory);
        if (Direction == ListSortDirection.Descending)
            comparison *= -1;

        // Override the sorting altogether when you find the special row value
        if (String.IsNullOrWhiteSpace(valorx))
            comparison = -1;
        else
        if (String.IsNullOrWhiteSpace(valory))
            comparison = 1;

        return comparison;
    }
}