Exemple #1
0
        public void SortByColumn(SortableGridViewColumn sortableGridViewColumn, ListSortDirection direction)
        {
            bool newSortColumn = false;

            // determine if this is a new sort, or a switch in sort direction.
            if (lastSortedOnColumn == null ||
                String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName) ||
                !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
            {
                newSortColumn = true;
            }

            // get the sort property name from the column's information.
            string sortPropertyName = sortableGridViewColumn.SortPropertyName;

            // Sort the data.
            Sort(sortPropertyName, direction);

            if (direction == ListSortDirection.Ascending)
            {
                if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate))
                {
                    sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate;
                }
                else
                {
                    sortableGridViewColumn.HeaderTemplate = null;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(this.ColumnHeaderSortedDescendingTemplate))
                {
                    sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedDescendingTemplate) as DataTemplate;
                }
                else
                {
                    sortableGridViewColumn.HeaderTemplate = null;
                }
            }

            // Remove arrow from previously sorted header
            if (newSortColumn && lastSortedOnColumn != null)
            {
                if (!String.IsNullOrEmpty(this.ColumnHeaderNotSortedTemplate))
                {
                    lastSortedOnColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderNotSortedTemplate) as DataTemplate;
                }
                else
                {
                    lastSortedOnColumn.HeaderTemplate = null;
                }
            }
            lastSortedOnColumn = sortableGridViewColumn;
        }
Exemple #2
0
        /// <summary>
        /// Executes when the control is initialized completely the first time through. Runs only once.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialized(EventArgs e)
        {
            // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
            this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));

            // cast the ListView's View to a GridView
            GridView gridView = this.View as GridView;

            if (gridView != null)
            {
                // determine which column is marked as IsDefaultSortColumn. Stops on the first column marked this way.
                SortableGridViewColumn sortableGridViewColumn = null;
                foreach (GridViewColumn gridViewColumn in gridView.Columns)
                {
                    sortableGridViewColumn = gridViewColumn as SortableGridViewColumn;
                    if (sortableGridViewColumn != null)
                    {
                        if (sortableGridViewColumn.IsDefaultSortColumn)
                        {
                            break;
                        }
                        sortableGridViewColumn = null;
                    }
                }

                // if the default sort column is defined, sort the data and then update the templates as necessary.
                if (sortableGridViewColumn != null)
                {
                    lastSortedOnColumn = sortableGridViewColumn;
                    Sort(sortableGridViewColumn.SortPropertyName, ListSortDirection.Ascending);

                    if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate))
                    {
                        sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate;
                    }

                    this.SelectedIndex = 0;
                }
            }

            base.OnInitialized(e);
        }
Exemple #3
0
        /// <summary>
        /// Event Handler for the ColumnHeader Click Event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

            // ensure that we clicked on the column header and not the padding that's added to fill the space.
            if (headerClicked != null && headerClicked.Role != GridViewColumnHeaderRole.Padding)
            {
                // attempt to cast to the sortableGridViewColumn object.
                SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;

                // ensure that the column header is the correct type and a sort property has been set.
                if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
                {
                    ListSortDirection direction;

                    if (lastSortedOnColumn == null ||
                        String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName) ||
                        !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        //This is a new sort, start with Ascending
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }
                    SortByColumn(sortableGridViewColumn, direction);
                }
            }
        }
Exemple #4
0
        public void ApplySettings(GridSettings settings)
        {
            GridView gridView = this.View as GridView;

            if (gridView != null)
            {
                if (settings.ColumnSettings != null)
                {
                    int firstIndex = gridView.Columns.IndexOf(gridView.Columns.OfType <SortableGridViewColumn>().First());
                    var columns    = gridView.Columns.OfType <SortableGridViewColumn>().ToLookup(column => column.SortPropertyName);
                    //First, apply position and size of all columns in the settings
                    for (int i = 0; i < settings.ColumnSettings.Length; i++)
                    {
                        GridSettings.ColumnSetting columnSetting = settings.ColumnSettings[i];
                        SortableGridViewColumn     column        = columns[columnSetting.Name].FirstOrDefault();
                        if (column != null)
                        {
                            //There is a column to which these settings apply.
                            column.Width     = columnSetting.Width;
                            column.IsVisible = columnSetting.IsVisible;
                            //Move the column to the right index
                            gridView.Columns.Move(gridView.Columns.IndexOf(column), firstIndex + i);
                        }
                    }

                    if (settings.LastSortedOnColumnIndex > -1 && settings.LastSortedOnColumnIndex < gridView.Columns.Count)
                    {
                        SortableGridViewColumn lastSortedColumn = gridView.Columns[settings.LastSortedOnColumnIndex] as SortableGridViewColumn;
                        if (lastSortedColumn != null)
                        {
                            SortByColumn(lastSortedColumn, settings.LastDirection);
                        }
                    }
                }
            }
        }