/// <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; if (_curSortCol != null) { AdornerLayer.GetAdornerLayer(_curSortCol).Remove(_curAdorner); } // 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; // 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)) { direction = ListSortDirection.Ascending; } else { direction = (_lastSortDirection == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending; } Cursor oldCursor = Cursor; try { Cursor = Cursors.Wait; FirePreviewSortCompletedEvent( ); Sort(sortableGridViewColumn.SortPropertyName, direction); SetAdorner(headerClicked, direction); FireSortCompletedEvent( ); } finally { Cursor = oldCursor; } _lastSortedOnColumn = sortableGridViewColumn; } } }
/// <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 ); // GridViewColumnHeader header = (GridViewColumnHeader)sortableGridViewColumn; // SetAdorner( header, ListSortDirection.Ascending ); //if( !String.IsNullOrEmpty( this.ColumnHeaderSortedAscendingTemplate ) ) //{ // sortableGridViewColumn.HeaderTemplate = this.TryFindResource( ColumnHeaderSortedAscendingTemplate ) as DataTemplate; //} // this.SelectedIndex = 0; // } } base.OnInitialized(e); }