예제 #1
0
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            bool isMouseCaptured = this.IsMouseCaptured;
            bool isPressed       = this.IsPressed;

            if (m_dragSourceManager != null)
            {
                m_dragSourceManager.ProcessMouseLeftButtonUp(e);
            }

            if (isMouseCaptured)
            {
                bool click = isPressed;

                if (click)
                {
                    bool           allowSort = true;
                    GroupByControl parentGBC = this.GetParentGroupByControl();

                    if (parentGBC != null)
                    {
                        allowSort = parentGBC.AllowSort;
                    }

                    if (allowSort)
                    {
                        DataGridContext       gridContext = DataGridControl.GetDataGridContext(this);
                        GroupLevelDescription groupInfo   = this.Content as GroupLevelDescription;
                        Debug.Assert((gridContext != null) && (groupInfo != null));

                        if ((gridContext != null) && (groupInfo != null))
                        {
                            ColumnBase column = gridContext.Columns[groupInfo.FieldName];

                            if ((column != null) && (column.AllowSort))
                            {
                                DataGridContext           dataGridContext  = DataGridControl.GetDataGridContext(this);
                                SortDescriptionCollection sortDescriptions = dataGridContext.Items.SortDescriptions;
                                ColumnCollection          columns          = dataGridContext.Columns;

                                SortingHelper.ToggleColumnSort(
                                    dataGridContext, sortDescriptions,
                                    columns, column, ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift));

                                e.Handled = true;
                            }
                        }
                    }
                }
            }

            base.OnMouseLeftButtonUp(e);
        }
예제 #2
0
        internal void DoSort(bool shiftUnpressed)
        {
            if (!this.CanDoSort())
            {
                return;
            }

            DataGridContext dataGridContext = this.DataGridContext;

            Debug.Assert(dataGridContext != null);

            SortingHelper.ToggleColumnSort(
                dataGridContext, dataGridContext.Items.SortDescriptions,
                dataGridContext.Columns, this.ParentColumn, shiftUnpressed);
        }
예제 #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);
            }
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            bool isMouseCaptured = this.IsMouseCaptured;
            bool isPressed       = this.IsPressed;

            if (m_dragSourceManager != null)
            {
                m_dragSourceManager.ProcessMouseLeftButtonUp(e);
            }

            if (isMouseCaptured)
            {
                bool click = isPressed;

                if (click)
                {
                    // DataGridCollectionView always return true for CanSort
                    bool allowSort = true;

                    // Use the ParentDataGridContext to be sure to get a
                    // DataGridContext of the correct detail level since
                    // all the HierarchicalGroupByItem will share the same DataGridContext
                    // which is the one of the level where the HierarchicalGroupByControl
                    // is located
                    DataGridContext dataGridContext;

                    try
                    {
                        dataGridContext = this.ParentDataGridContext;
                    }
                    catch (DataGridInternalException)
                    {
                        dataGridContext = null;
                    }

                    if ((dataGridContext != null) && (dataGridContext.SourceDetailConfiguration == null))
                    {
                        allowSort = dataGridContext.Items.CanSort;
                    }

                    if (allowSort)
                    {
                        ColumnCollection      columns   = this.ParentColumns;
                        GroupLevelDescription groupInfo = this.Content as GroupLevelDescription;

                        Debug.Assert((columns != null) && (groupInfo != null));

                        if ((columns != null) && (groupInfo != null))
                        {
                            ColumnBase column = columns[groupInfo.FieldName];

                            if ((column != null) && (column.AllowSort))
                            {
                                SortingHelper.ToggleColumnSort(
                                    dataGridContext, this.ParentSortDescriptions,
                                    columns, column, ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift));

                                e.Handled = true;
                            }
                        }
                    }
                }
            }

            base.OnMouseLeftButtonUp(e);
        }
예제 #5
0
        internal static void ToggleColumnSort(
            DataGridContext dataGridContext,
            SortDescriptionCollection sortDescriptions,
            ColumnCollection columns,
            ColumnBase column,
            bool shiftUnpressed)
        {
            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 can't be null.");
            }

            DataGridContext parentDataGridContext = (dataGridContext == null) ? null : dataGridContext.ParentDataGridContext;

            // Defer the RestoreState of each DataGridContext of the same level
            // to ensure all the DataGridContext will be correctly restored once
            // all of them are completely resorted
            HashSet <IDisposable> deferRestoreStateDisposable = new HashSet <IDisposable>();

            if (parentDataGridContext != null)
            {
                foreach (DataGridContext childContext in parentDataGridContext.GetChildContexts())
                {
                    deferRestoreStateDisposable.Add(childContext.DeferRestoreState());
                }
            }

            IDisposable deferResortHelper = (dataGridContext == null) ? null :
                                            SortingHelper.DeferResortHelper(dataGridContext.ItemsSourceCollection, dataGridContext.Items, sortDescriptions);

            //this will ensure that all DataGridCollectionViews mapped to this SortDescriptions collection will only refresh their sorting once!
            SortDirection newSortDirection = column.SortDirection;

            if ((shiftUnpressed) &&
                ((column.SortIndex == -1) || (sortDescriptions.Count > 1)))
            {
                sortDescriptions.Clear();
            }

            switch (newSortDirection)
            {
            case SortDirection.None:
                newSortDirection = SortDirection.Ascending;
                break;

            case SortDirection.Ascending:
                newSortDirection = SortDirection.Descending;
                break;

            case SortDirection.Descending:
                newSortDirection = SortDirection.None;
                break;
            }

            SortingHelper.ApplyColumnSort(dataGridContext, sortDescriptions, columns, column, newSortDirection);

            if (deferResortHelper != null)
            {
                //end of the DeferResort(), any DataGridCollectionView listening to the SortDescriptions instance will refresh its sorting!
                deferResortHelper.Dispose();
            }

            foreach (IDisposable disposable in deferRestoreStateDisposable)
            {
                try
                {
                    // Try/Catch to ensure all contexts are restored
                    disposable.Dispose();
                }
                catch (Exception)
                {
                }
            }

            deferRestoreStateDisposable.Clear();
        }