Exemple #1
0
        public DataGridPage()
        {
            this.InitializeComponent();

            var dataGridSortDescription = DataGridSortDescription.FromPath(nameof(Country.Region), ListSortDirection.Ascending, new ReversedStringComparer());
            var collectionView1         = new DataGridCollectionView(Countries.All);

            collectionView1.SortDescriptions.Add(dataGridSortDescription);
            var dg1 = this.Get <DataGrid>("dataGrid1");

            dg1.IsReadOnly  = true;
            dg1.LoadingRow += Dg1_LoadingRow;
            dg1.Sorting    += (s, a) =>
            {
                var binding = (a.Column as DataGridBoundColumn)?.Binding as Binding;

                if (binding?.Path is string property &&
                    property == dataGridSortDescription.PropertyPath &&
                    !collectionView1.SortDescriptions.Contains(dataGridSortDescription))
                {
                    collectionView1.SortDescriptions.Add(dataGridSortDescription);
                }
            };
            dg1.Items = collectionView1;

            var dg2 = this.Get <DataGrid>("dataGridGrouping");

            dg2.IsReadOnly = true;

            var collectionView2 = new DataGridCollectionView(Countries.All);

            collectionView2.GroupDescriptions.Add(new DataGridPathGroupDescription("Region"));

            dg2.Items = collectionView2;

            var dg3 = this.Get <DataGrid>("dataGridEdit");

            dg3.IsReadOnly = false;

            var items = new List <Person>
            {
                new Person {
                    FirstName = "John", LastName = "Doe", Age = 30
                },
                new Person {
                    FirstName = "Elizabeth", LastName = "Thomas", IsBanned = true, Age = 40
                },
                new Person {
                    FirstName = "Zack", LastName = "Ward", Age = 50
                }
            };
            var collectionView3 = new DataGridCollectionView(items);

            dg3.Items = collectionView3;

            var addButton = this.Get <Button>("btnAdd");

            addButton.Click += (a, b) => collectionView3.AddNew();
        }
        public void OrderBy_Orders_Correctly_When_Descending()
        {
            var items = new[]
            {
                new Item("b", "b"),
                new Item("a", "a"),
                new Item("c", "c"),
            };
            var expectedResult  = items.OrderByDescending(i => i.Prop1).ToList();
            var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), @descending: true);

            sortDescription.Initialize(typeof(Item));
            var result = sortDescription.OrderBy(items).ToList();

            Assert.Equal(expectedResult, result);
        }
        public void ThenBy_Orders_Correctly_When_Descending()
        {
            // Casting nonsense below because IOrderedEnumerable<T> isn't covariant in full framework and we need an
            // object of type IOrderedEnumerable<object> for DataGridSortDescription.ThenBy
            var items = new[]
            {
                (object)new Item("a", "b"),
                new Item("a", "a"),
                new Item("a", "c"),
            }.OrderBy(i => ((Item)i).Prop1);
            var expectedResult = new[]
            {
                new Item("a", "c"),
                new Item("a", "b"),
                new Item("a", "a"),
            };
            var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), @descending: true);

            sortDescription.Initialize(typeof(Item));
            var result = sortDescription.ThenBy(items).ToList();

            Assert.Equal(expectedResult, result);
        }
Exemple #4
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);
                        }
                    }
                }
            }
        }