修复DataGrid中的第一行

时间:2015-03-27 09:41:29

标签: wpf datagrid

我试图将DataGrid中的一行始终保持在顶部,同时仍然能够按列(int和string列)对所有其他行进行排序。

我的数据结构提供了一些帮助:第一列被命名为“Id”,而我试图保持在顶部的行总是具有所有ID中最低的Id。此行包含聚合值。

典型的DataGrid可能如下所示:

ID | Name | Result1 | Result2
5  | avg  |  2      | 5
6  | opt1 |  1      | 3
7  | opt2 |  3      | 7

可能有n列,列数将在运行时更改。 DataGrid绑定到ListCollectionView,我还实现了基于trilson86's answer的自定义排序器:

    <DataGrid result:CustomSortBehaviour.AllowCustomSort="True" 
              IsReadOnly="True" 
              ItemsSource="{Binding ResultDataView}">
    </DataGrid>

到目前为止,使用trilson86的方法,我在排序时设法保持第一线。这是我的CustomSortBehavior类中的处理程序,它为自定义排序器准备有用的数据块(例如当前DataGrid中的minimum-Id):

    private static void HandleCustomSorting(object sender, DataGridSortingEventArgs e)
    {
        var dataGrid = sender as DataGrid;
        if (dataGrid == null || !GetAllowCustomSort(dataGrid)) return;
        var listColView = dataGrid.ItemsSource as ListCollectionView;
        var min = listColView.Cast<DataRowView>().Min(x => x.Row[0]);
        var sorter = new MyComparer();
        e.Handled = true;
        var direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
        e.Column.SortDirection = sorter.SortDirection = direction;
        sorter.IdOfFirstRow = Convert.ToInt32(min);
        listColView.CustomSort = sorter;
    }

自定义分拣机本身:

    public int Compare(object x, object y)
    {
        var rowView1 = x as DataRowView;
        var rowView2 = y as DataRowView;
        var row1 = rowView1.Row;
        var row2 = rowView2.Row;
        var row1Id = Convert.ToInt32(row1[0]);
        var row2Id = Convert.ToInt32(row2[0]);

        if (row1IdValue == IdOfFirstRow)
            return -1;

        if (row2IdValue == IdOfFirstRow)
            return 1;

        if (SortDirection == ListSortDirection.Ascending) {
            return row2Id.CompareTo(row1Id); 
        }
        else
        {
            return row1Id.CompareTo(row2Id);
        }
    }

这只是解决方案的一半..按原样硬编码,我只能按Id排序。因为列将在运行时添加,所以我不能在设计时定义所有列并根据列值类型(int或string)附加分类器。

如何保持所有其他列的排序,同时保持具有最小Id的行的限制保持在最顶层?

1 个答案:

答案 0 :(得分:1)

为什么不使用DisplayIndex属性来获取要排序的列。

private static void HandleCustomSorting(object sender, DataGridSortingEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    if (dataGrid == null || !GetAllowCustomSort(dataGrid)) return;
    ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
    ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(dataGrid.ItemSource);
    int min = lcv.Cast<DataRowView>().Min(x => x.Row[0]);

    lcv.CustomSort = new CustomComparer(direction, e.Column.DisplayIndex, min); //DisplayIndex gets you your column
    e.Handled = true;
}

然后这个比较器应该做你想要的,而不是在排序ints和列中的字符串时排序min ID行。

public class CustomComparer : IComparer
{
     ListSortDirection _direction;
     int colNum;
     int _IdOfFirstRow;
     public CustomComparer(ListSortDirection direction, int colNum, int IdOfFirstRow)
     {
          _direction = _direction;
          _colNum = colNum;
          _IdOfFirstRow = IdOfFirstRow;
     }
     public int Compare(object x, object y)
     {
          DataRowView rowView1 = x as DataRowView;
          DataRowView rowView2 = y as DataRowView;
          int valX, valY;
          if (x == y)
             return 0;

          //Don't sort min Id
          var row1Id = Convert.ToInt32(rowView1[0]);
          var row2Id = Convert.ToInt32(rowView2[0]);

          if (row1Id == _IdOfFirstRow) 
              return -1;
          else if (row2Id == _IdOfFirstRow)
              return 1;

          string strX = rowView1[_colNum] as string;
          string strY = rowView2[_colNum] as string;
          bool ret1 = int.TryParse(strX, valX);
          bool ret2 = int.TryParse(strY, valY);

          if (ret1 == false && ret2 == false) //is a string
          {

               if (_direction == ListSortDirection.Ascending)
               {
                    return strX.CompareTo(strY);
               }
               else
               {
                    return strY.CompareTo(strX);
               }
          }
          else
          {
               if (_direction == ListSortDirection.Ascending)
               {
                    return valX.CompareTo(valY);
               }
               else
               {
                    return valY.CompareTo(valX);
               }
          }
     }
}