internal static void ShowColumn(SortableGridViewColumn column, bool visible) { if (visible) { column.ClearValue(GridViewColumn.WidthProperty); } else { column.Width = 0.0; } }
private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e) { if (this.ItemsSource == null) { return; } 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) { return; } // 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)) { return; } ListSortDirection direction = ListSortDirection.Ascending; // determine if this is a new sort, bool newSortColumn = this.lastSortedOnColumn == null || string.IsNullOrEmpty(this.lastSortedOnColumn.SortPropertyName) || !string.Equals(sortableGridViewColumn.SortPropertyName, this.lastSortedOnColumn.SortPropertyName, StringComparison.OrdinalIgnoreCase); // if not switch sort direction. if (!newSortColumn) { direction = (this.lastDirection == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending; } // Sort the data. this.Sort(sortableGridViewColumn, direction); string template = (direction == ListSortDirection.Ascending) ? this.ColumnHeaderSortedAscendingTemplate : this.ColumnHeaderSortedDescendingTemplate; sortableGridViewColumn.HeaderTemplate = !string.IsNullOrEmpty(template) ? this.TryFindResource(template) as DataTemplate : null; // Remove arrow from previously sorted header if (newSortColumn && this.lastSortedOnColumn != null) { this.lastSortedOnColumn.HeaderTemplate = !string.IsNullOrEmpty(this.ColumnHeaderNotSortedTemplate) ? this.TryFindResource(this.ColumnHeaderNotSortedTemplate) as DataTemplate : null; } this.lastSortedOnColumn = sortableGridViewColumn; }
/// <summary> /// Executes when the control is initialized completely the first time through. Runs only once. /// </summary> /// <param name="e">EventArgs</param> protected override void OnInitialized(EventArgs e) { Loaded += this.OnLoaded; // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView. this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(this.GridViewColumnHeaderClickedHandler)); var defaultDirection = ListSortDirection.Ascending; // 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) { defaultDirection = sortableGridViewColumn.DefaultSortDirection; break; } sortableGridViewColumn = null; } } // if the default sort column is defined, sort the data and then update the templates as necessary. if (sortableGridViewColumn != null) { this.lastSortedOnColumn = sortableGridViewColumn; this.Sort(sortableGridViewColumn, defaultDirection); if (!string.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate)) { sortableGridViewColumn.HeaderTemplate = this.TryFindResource(this.ColumnHeaderSortedAscendingTemplate) as DataTemplate; } } } base.OnInitialized(e); }
private void Sort(SortableGridViewColumn sortableGridViewColumn, ListSortDirection direction) { this.lastDirection = direction; ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ItemsSource); if (dataView == null) { return; } dataView.SortDescriptions.Clear(); // get the sort property name from the column's information, add it as the first parameter SortDescription sd = new SortDescription(sortableGridViewColumn.SortPropertyName, direction); dataView.SortDescriptions.Add(sd); dataView.Refresh(); }
/// <summary> /// Executes when the control is initialized completely the first time through. Runs only once. /// </summary> /// <param name="e">EventArgs</param> protected override void OnInitialized(EventArgs e) { this.Loaded += this.OnLoaded; // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView. this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(this.GridViewColumnHeaderClickedHandler)); var defaultDirection = ListSortDirection.Ascending; // 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) { defaultDirection = sortableGridViewColumn.DefaultSortDirection; break; } sortableGridViewColumn = null; } } // if the default sort column is defined, sort the data and then update the templates as necessary. if (sortableGridViewColumn != null) { this.lastSortedOnColumn = sortableGridViewColumn; this.Sort(sortableGridViewColumn, defaultDirection); if (!string.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate)) { sortableGridViewColumn.HeaderTemplate = this.TryFindResource(this.ColumnHeaderSortedAscendingTemplate) as DataTemplate; } } } base.OnInitialized(e); }