Esempio n. 1
0
        private static void ApplyColumnSort(DataGridContext dataGridContext, SortDescriptionCollection sortDescriptions, ColumnCollection columns, ColumnBase column, SortDirection sortDirection)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }

            Debug.Assert((dataGridContext != null) || (column.ParentDetailConfiguration != null));

            if ((dataGridContext == null) && (column.ParentDetailConfiguration == null))
            {
                throw new DataGridInternalException("DataGridContext or ParentDetailConfiguration cannot be null.");
            }

            if (!columns.Contains(column))
            {
                throw new ArgumentException("The specified column is not part of the DataGridContext.", "column");
            }

            string fieldName = column.FieldName;

            bool canSort = (dataGridContext != null) ? dataGridContext.Items.CanSort : true;

            if ((!string.IsNullOrEmpty(fieldName)) && (canSort == true))
            {
                SortDescription existingSort;
                int             sortDescriptionIndex = sortDescriptions.Count;

                for (int i = sortDescriptions.Count - 1; i >= 0; i--)
                {
                    existingSort = sortDescriptions[i];

                    if (existingSort.PropertyName == fieldName)
                    {
                        if (((existingSort.Direction == ListSortDirection.Ascending) && (sortDirection == SortDirection.Ascending)) ||
                            ((existingSort.Direction == ListSortDirection.Descending) && (sortDirection == SortDirection.Descending)))
                        {
                            // Nothing to change!
                            sortDescriptionIndex = -1;
                        }
                        else
                        {
                            sortDescriptionIndex = i;
                            sortDescriptions.Remove(existingSort);
                        }

                        break;
                    }
                }

                int maxDescriptions = (dataGridContext != null) ? dataGridContext.MaxSortLevels
          : column.ParentDetailConfiguration.MaxSortLevels;

                if ((maxDescriptions != -1) && (sortDescriptions.Count >= maxDescriptions))
                {
                    // Cannot insert sort description since it would go beyond the max sort description count.
                    sortDescriptionIndex = -1;
                    sortDirection        = SortDirection.None;
                }

                if ((sortDescriptionIndex > -1) && (sortDirection != SortDirection.None))
                {
                    SortDescription sortDescription = new SortDescription(fieldName,
                                                                          (sortDirection == SortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending);

                    sortDescriptions.Insert(sortDescriptionIndex, sortDescription);
                    column.SetSortIndex(sortDescriptionIndex);
                    column.SetSortDirection(sortDirection);
                }

                SortingHelper.SynchronizeSortIndexes(sortDescriptions, columns);
            }
        }