/// /// 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) { SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn; // attempt to cast to the sortableGridViewColumn object. var dataView = CollectionViewSource.GetDefaultView(this.ItemsSource); // ensure that the column header is the correct type and a sort property has been set. if (sortableGridViewColumn != null) { // get the sort property name from the column's information. string sortPropertyName = sortableGridViewColumn.SortPropertyName; if (!String.IsNullOrEmpty(sortPropertyName)) { var lastDirection = sortableGridViewColumn.SortDirection; ListSortDirection direction = SortableGridViewColumn.NoSort; // determine if this is a new sort, or a switch in sort direction. if (lastDirection == ListSortDirection.Ascending) { direction = ListSortDirection.Descending; } else if (lastDirection == ListSortDirection.Descending) { direction = SortableGridViewColumn.NoSort; } else { direction = ListSortDirection.Ascending; } Label sortIndicator = (Label)BaseWPFHelpers.SingleFindDownInTree(headerClicked, new BaseWPFHelpers.FinderMatchName("sortIndicator")); // Sort the data. Sort(sortPropertyName, direction); if (direction == SortableGridViewColumn.NoSort) { sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"]; } else { if (direction == ListSortDirection.Ascending) { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowUp"]; } else { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowDown"]; } } sortableGridViewColumn.SortDirection = direction; } } } }
void SortableListView_Loaded(object sender, RoutedEventArgs e) { FindTheHeaders(); // 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.SortDirection != SortableGridViewColumn.NoSort && !string.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName)) { var dataView = CollectionViewSource.GetDefaultView(this.ItemsSource); if (dataView != null) { dataView.SortDescriptions.Add(new SortDescription(sortableGridViewColumn.SortPropertyName, sortableGridViewColumn.SortDirection)); GridViewColumnHeader header = m_Headers[sortableGridViewColumn]; if (header != null) { ListSortDirection direction = sortableGridViewColumn.SortDirection; Label sortIndicator = (Label)BaseWPFHelpers.SingleFindDownInTree(header, new BaseWPFHelpers.FinderMatchName("sortIndicator")); if (direction == SortableGridViewColumn.NoSort) { sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"]; } else { if (direction == ListSortDirection.Ascending) { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowUp"]; } else { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowDown"]; } } } //if (sortableGridViewColumn.SortDirection == ListSortDirection.Ascending) //{ // if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate)) // { // sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate; // } //} //else //{ // if (!String.IsNullOrEmpty(this.ColumnHeaderSortedDescendingTemplate)) // { // sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedDescendingTemplate) as DataTemplate; // } //} } } } } } }