Exemplo n.º 1
0
        //TODO GroupSorting
        internal void ProcessSort(KeyModifiers keyModifiers)
        {
            // if we can sort:
            //  - DataConnection.AllowSort is true, and
            //  - AllowUserToSortColumns and CanSort are true, and
            //  - OwningColumn is bound, and
            //  - SortDescriptionsCollection exists, and
            //  - the column's data type is comparable
            // then try to sort
            if (OwningColumn != null &&
                OwningGrid != null &&
                OwningGrid.EditingRow == null &&
                OwningColumn != OwningGrid.ColumnsInternal.FillerColumn &&
                OwningGrid.DataConnection.AllowSort &&
                OwningGrid.CanUserSortColumns &&
                OwningColumn.CanUserSort &&
                OwningGrid.DataConnection.SortDescriptions != null)
            {
                DataGrid owningGrid = OwningGrid;

                DataGridSortDescription newSort;

                KeyboardHelper.GetMetaKeyState(keyModifiers, out bool ctrl, out bool shift);

                DataGridSortDescription sort           = OwningColumn.GetSortDescription();
                IDataGridCollectionView collectionView = owningGrid.DataConnection.CollectionView;
                Debug.Assert(collectionView != null);
                using (collectionView.DeferRefresh())
                {
                    // if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
                    if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0)
                    {
                        owningGrid.DataConnection.SortDescriptions.Clear();
                    }

                    // if ctrl is held down, we only clear the sort directions
                    if (!ctrl)
                    {
                        if (sort != null)
                        {
                            newSort = sort.SwitchSortDirection();

                            // changing direction should not affect sort order, so we replace this column's
                            // sort description instead of just adding it to the end of the collection
                            int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort);
                            if (oldIndex >= 0)
                            {
                                owningGrid.DataConnection.SortDescriptions.Remove(sort);
                                owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort);
                            }
                            else
                            {
                                owningGrid.DataConnection.SortDescriptions.Add(newSort);
                            }
                        }
                        else
                        {
                            string propertyName = OwningColumn.GetSortPropertyName();
                            // no-opt if we couldn't find a property to sort on
                            if (string.IsNullOrEmpty(propertyName))
                            {
                                return;
                            }

                            newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture);
                            owningGrid.DataConnection.SortDescriptions.Add(newSort);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        //TODO GroupSorting
        internal void ProcessSort(InputModifiers inputModifiers)
        {
            // if we can sort:
            //  - DataConnection.AllowSort is true, and
            //  - AllowUserToSortColumns and CanSort are true, and
            //  - OwningColumn is bound, and
            //  - SortDescriptionsCollection exists, and
            //  - the column's data type is comparable
            // then try to sort
            if (OwningColumn != null
                && OwningGrid != null
                && OwningGrid.EditingRow == null
                && OwningColumn != OwningGrid.ColumnsInternal.FillerColumn
                && OwningGrid.DataConnection.AllowSort
                && OwningGrid.CanUserSortColumns
                && OwningColumn.CanUserSort
                && OwningGrid.DataConnection.SortDescriptions != null)
            {
                DataGrid owningGrid = OwningGrid;
                bool desending;
                AvaloniaSortDescription newSort;

                KeyboardHelper.GetMetaKeyState(inputModifiers, out bool ctrl, out bool shift);

                AvaloniaSortDescription sort = OwningColumn.GetSortDescription();
                ICollectionView collectionView = owningGrid.DataConnection.CollectionView;
                Debug.Assert(collectionView != null);
                using (collectionView.DeferRefresh())
                {
                    // if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
                    if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0)
                    {
                        /*if (collectionView.CanGroup && collectionView.GroupDescriptions != null)
                        {
                            // Make sure we sort by the GroupDescriptions first
                            for (int i = 0; i < collectionView.GroupDescriptions.Count; i++)
                            {
                                PropertyGroupDescription groupDescription = collectionView.GroupDescriptions[i] as PropertyGroupDescription;
                                if (groupDescription != null && collectionView.SortDescriptions.Count <= i || collectionView.SortDescriptions[i].PropertyName != groupDescription.PropertyName)
                                {
                                    collectionView.SortDescriptions.Insert(Math.Min(i, collectionView.SortDescriptions.Count), new SortDescription(groupDescription.PropertyName, ListSortDirection.Ascending));
                                }
                            }
                            while (collectionView.SortDescriptions.Count > collectionView.GroupDescriptions.Count)
                            {
                                collectionView.SortDescriptions.RemoveAt(collectionView.GroupDescriptions.Count);
                            }
                        }
                        else if (!shift)
                        {
                            owningGrid.DataConnection.SortDescriptions.Clear();
                        }*/
                        owningGrid.DataConnection.SortDescriptions.Clear();
                    }

                    if (sort != null)
                    {
                        newSort = sort.SwitchSortDirection();

                        // changing direction should not affect sort order, so we replace this column's
                        // sort description instead of just adding it to the end of the collection
                        int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort);
                        if (oldIndex >= 0)
                        {
                            owningGrid.DataConnection.SortDescriptions.Remove(sort);
                            owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort);
                        }
                        else
                        {
                            owningGrid.DataConnection.SortDescriptions.Add(newSort);
                        }
                    }
                    else
                    {
                        string propertyName = OwningColumn.GetSortPropertyName();
                        // no-opt if we couldn't find a property to sort on
                        if (string.IsNullOrEmpty(propertyName))
                        {
                            return;
                        }

                        newSort = AvaloniaSortDescription.FromPath(propertyName, culture: collectionView.Culture);
                        owningGrid.DataConnection.SortDescriptions.Add(newSort);
                    }
                }

                // We've completed the sort, so send the Invoked event for the column header's automation peer
                /*if (AutomationPeer.ListenerExists(AutomationEvents.InvokePatternOnInvoked))
                {
                    AutomationPeer peer = FrameworkElementAutomationPeer.FromElement(this);
                    if (peer != null)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
                    }
                }*/

            }
        }