группировка по порядку?

В WPF я могу группировать, но по умолчанию используется группа по возрастанию. Одна из моих потребностей, чтобы иметь возможность контролировать сортировку группы (по возрастанию или по убыванию). Например:

группа 1

  • пункт 1.1
  • пункт 1.2
  • пункт 1.3

группа 2

  • пункт 2.1
  • пункт 2.2

а также иметь возможность переключаться на:

группа 2

  • пункт 2.1
  • пункт 2.2

группа 1

  • пункт 1.1
  • пункт 1.2
  • пункт 1.3

    //Here is the function to setup a group for a particular column:
    private void SetupGrouping(DataGrid parentGrid, DataGridColumn col)
    {
        if (parentGrid == null || col == null || string.IsNullOrEmpty(col.SortMemberPath))
            return;
    
        ICollectionView vw = GetDefaultView();
        if (vw != null && vw.CanGroup)
        {
            if (vw.GroupDescriptions.Count != 0)
            {
                vw.GroupDescriptions.Clear();
            }
    
            PropertyGroupDescription gd = new PropertyGroupDescription(col.SortMemberPath);
    
            // Check to see if the column is Priority, if it is
            // then do the grouping with high priority (3) on top.
            // The order should be High(3), Normal (2), Low(1)
            DataGridColumn priCol = GetColumnByID(ColumnFlags.Priority);
            if(col == priCol)
            {
                // Attempted to change the direction of the sort added by adding group.
                // However, it has error complaining SortDescription is sealed 
                // and can't be changed.
                //if (vw.SortDescriptions != null && vw.SortDescriptions.Count > 0)
                //{
                //    SortDescription sd = vw.SortDescriptions[0];
                //    if (sd.PropertyName == col.SortMemberPath)
                //    {
                //        sd.Direction = ListSortDirection.Descending;
                //    }
                //}
            }
    
            // Info: when we add a new GroupDescription to GroupDescriptions list, 
            // guest what? a new SortDescription is also added to the 
            // SortDescriptions list.
            vw.GroupDescriptions.Add(gd);
        }
    
        // Save off the column for later use
        GroupedColumn = col;
    
        // Set the DataGrid's Tag so that the GroupSyle can get the column name
        parentGrid.Tag = DispatchAttachedProperties.GetColumnHeader(col);
    }
    

person Community    schedule 04.08.2009    source источник


Ответы (1)


У тебя была правильная мысль. Добавьте SortDescription в ICollectionView на основе того же свойства, по которому вы группируете. Если вы хотите изменить направление сортировки, вам нужно очистить существующее и добавить новое для противоположного направления. Вы не можете изменить его после его создания, как вы обнаружили.

person Bryce Kahle    schedule 04.08.2009