Esempio n. 1
0
        private void UpdateSortDefinitions(DataGridColumn updatedColumn)
        {
            // Not nullable, check if there already is a sort description for that column.
            if (_sortDescriptions.Any(i => i.PropertyName == updatedColumn.SortMemberPath))
            {
                // Get the description.
                var columnSortDescription =
                    _sortDescriptions.FirstOrDefault(i => i.PropertyName == updatedColumn.SortMemberPath);

                var columnSortDirection = updatedColumn.SortDirection;

                // Always remove from definitions, updating value throws an exception.
                _sortDescriptions.Remove(columnSortDescription);

                // If there is a direction, add the new value.
                if (columnSortDirection != null)
                {
                    _sortDescriptions.Add(
                        new SortDescription(updatedColumn.SortMemberPath, GetSortDirection(updatedColumn)));
                }
            }

            // Add column to definitions.
            else
            {
                _sortDescriptions.Add(
                    new SortDescription(updatedColumn.SortMemberPath, GetSortDirection(updatedColumn)));
            }

            UpdateColumnSortDefinitions();
        }
    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 );
      }
    }
Esempio n. 3
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);
            }
        }