/// <summary> /// Handles the initialized event. If SortProperty is defined then the sort will be applied. /// </summary> /// <param name="e">The event arguments.</param> protected override void OnInitialized(EventArgs e) { // Handle the event when a header is clicked. this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(this.OnHeaderClicked)); if (this.SortProperty != null && !string.IsNullOrEmpty(this.SortProperty)) { GridView gridView = this.View as GridView; if (gridView != null) { foreach (SortGridViewColumn column in gridView.Columns) { if (column.SortProperty == this.SortProperty) { this.sortColumn = column; } } if (this.sortColumn != null) { this.SortList(); this.UpdateHeaderTemplate(); this.UpdateGroupTemplate(); } } } base.OnInitialized(e); }
/// <summary> /// A header was clicked. Sort and group by the associated column. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private void OnHeaderClicked(object sender, RoutedEventArgs e) { // Make sure the column is really being sorted. GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader; if (header == null || header.Role == GridViewColumnHeaderRole.Padding) { return; } SortGridViewColumn column = header.Column as SortGridViewColumn; if (column == null) { return; } // See if a new column was clicked, or the same column was clicked. if (this.sortColumn != column) { // A new column was clicked. this.previousSortColumn = this.sortColumn; this.sortColumn = column; this.SortDirection = ListSortDirection.Ascending; this.SortProperty = column.SortProperty; } else { // The same column was clicked, change the sort order. this.previousSortColumn = null; this.SortDirection = (this.SortDirection == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending; } this.UpdateHeaderTemplate(); this.UpdateGroupTemplate(); this.SortList(); }