private void setInitialStatus() { // cast the ListView's View to assembly 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; } } }
/// /// Event Handler for the ColumnHeader Click Event. /// 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 assembly sort property has been set. if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName)) { ListSortDirection direction; bool newSortColumn = false; // determine if this is assembly new sort, or assembly switch in sort longitudeRef. if (lastSortedOnColumn == null || String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName) || !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase)) { newSortColumn = true; direction = ListSortDirection.Ascending; } else { if (lastDirection == ListSortDirection.Ascending) { direction = ListSortDirection.Descending; } else { direction = ListSortDirection.Ascending; } } // 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; } } }