Ejemplo n.º 1
0
 public override bool Equals(object obj)
 {
     if (obj is SortData)
     {
         SortData other = obj as SortData;
         return(other.Index == Index && other.Order == Order);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 2
0
        private View GetHeaderViewForColumn(DataGridColumn column)
        {
            column.HeaderLabel.Style = column.HeaderLabelStyle ?? this.HeaderLabelStyle ?? (Style)_headerView.Resources["HeaderDefaultStyle"];
            View cell;

            if (column.HeaderTemplate != null)
            {
                cell = new ContentView {
                    Content = column.HeaderTemplate.CreateContent() as View
                };
            }
            else
            {
                Grid grid = new Grid
                {
                    ColumnSpacing = 0,
                };

                if (IsSortable)
                {
                    column.SortingIcon.Style             = (Style)_headerView.Resources["ImageStyleBase"];
                    column.SortingIcon.HorizontalOptions = LayoutOptions.End;

                    grid.Children.Add(column.SortingIcon);

                    TapGestureRecognizer tgr = new TapGestureRecognizer();
                    tgr.Tapped += (s, e) => {
                        int          index = Columns.IndexOf(column);
                        SortingOrder order = _sortingOrders[index] == SortingOrder.Ascendant ? SortingOrder.Descendant : SortingOrder.Ascendant;

                        if (Columns.ElementAt(index).SortingEnabled)
                        {
                            SortedColumnIndex = new SortData(index, order);
                        }
                    };
                    grid.GestureRecognizers.Add(tgr);
                }

                grid.Children.Add(column.HeaderLabel);

                cell = grid;
            }
            return(cell);
        }
        private View GetHeaderViewForColumn(DataGridColumn column)
        {
            column.HeaderLabel.Style = column.HeaderLabelStyle ?? this.HeaderLabelStyle ?? (Style)_headerView.Resources["HeaderDefaultStyle"];

            Grid grid = new Grid {
                ColumnSpacing = 0,
            };

            grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });
            grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Auto)
            });

            if (IsSortable)
            {
                column.SortingIcon.Style = (Style)_headerView.Resources["ImageStyleBase"];

                grid.Children.Add(column.SortingIcon);
                Grid.SetColumn(column.SortingIcon, 1);

                TapGestureRecognizer tgr = new TapGestureRecognizer();
                tgr.Tapped += (s, e) => {
                    int          index = Columns.IndexOf(column);
                    SortingOrder order = _sortingOrders[index] == SortingOrder.Ascendant ? SortingOrder.Descendant : SortingOrder.Ascendant;

                    if (Columns.ElementAt(index).SortingEnabled)
                    {
                        SortedColumnIndex = new SortData(index, order);
                    }
                };
                grid.GestureRecognizers.Add(tgr);
            }

            grid.Children.Add(column.HeaderLabel);

            return(grid);
        }
        private void SortItems(SortData sData)
        {
            if (InternalItems == null || sData.Index >= Columns.Count || !Columns[sData.Index].SortingEnabled)
            {
                return;
            }

            var          items  = InternalItems;
            var          column = Columns[sData.Index];
            SortingOrder order  = sData.Order;

            if (!IsSortable)
            {
                throw new InvalidOperationException("This DataGrid is not sortable");
            }
            else if (column.PropertyName == null)
            {
                throw new InvalidOperationException("Please set the PropertyName property of Column");
            }

            //Sort
            if (order == SortingOrder.Descendant)
            {
                items = items.OrderByDescending(x => ReflectionUtils.GetValueByPath(x, column.PropertyName)).ToList();
            }
            else
            {
                items = items.OrderBy(x => ReflectionUtils.GetValueByPath(x, column.PropertyName)).ToList();
            }

            column.SortingIcon.Style = (order == SortingOrder.Descendant) ?
                                       AscendingIconStyle ?? (Style)_headerView.Resources["DescendingIconStyle"] :
                                       DescendingIconStyle ?? (Style)_headerView.Resources["AscendingIconStyle"];

            //Support DescendingIcon property (if setted)
            if (!column.SortingIcon.Style.Setters.Any(x => x.Property == Image.SourceProperty))
            {
                if (order == SortingOrder.Descendant && DescendingIconProperty.DefaultValue != DescendingIcon)
                {
                    column.SortingIcon.Source = DescendingIcon;
                }
                if (order == SortingOrder.Ascendant && AscendingIconProperty.DefaultValue != AscendingIcon)
                {
                    column.SortingIcon.Source = AscendingIcon;
                }
            }

            for (int i = 0; i < Columns.Count; i++)
            {
                if (i != sData.Index)
                {
                    if (Columns[i].SortingIcon.Style != null)
                    {
                        Columns[i].SortingIcon.Style = null;
                    }
                    if (Columns[i].SortingIcon.Source != null)
                    {
                        Columns[i].SortingIcon.Source = null;
                    }
                    _sortingOrders[i] = SortingOrder.None;
                }
            }

            _internalItems = items;

            _sortingOrders[sData.Index] = order;
            SortedColumnIndex           = sData;

            _listView.ItemsSource = _internalItems;
        }