/// <summary> /// Initializes a new instance of the ColumnDescription class. /// </summary> /// <param name="columnViewColumn">The ColumnViewColumn used as a source for this shallow clone.</param> public ColumnDescription(ColumnViewColumn columnViewColumn) { // Initialize the object this.columnField = columnViewColumn; this.descriptionField = columnViewColumn.Description; this.isVisibleField = columnViewColumn.IsVisible; this.widthField = columnViewColumn.Width; }
/// <summary> /// Sizes the column to fit the largest cell. /// </summary> /// <param name="columnViewColumn">The column to be autosized.</param> void AutoSizeColumn(ColumnViewColumn columnViewColumn) { // This will create the same element that is used by the ColumnView templates. FrameworkElement frameworkElement = ColumnViewRowPresenter.CreateCell(columnViewColumn); // Now that we have the destination element we're going to pump all of the content through it and measure the maximum width of the columns. Double maxWidth = Double.MinValue; foreach (Object item in this.ListView.Items) { frameworkElement.DataContext = item; frameworkElement.UpdateLayout(); frameworkElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); maxWidth = Math.Max(frameworkElement.DesiredSize.Width, maxWidth); } // This will set the column to the maximum width calculated above. columnViewColumn.Width = maxWidth; }
/// <summary> /// Handles a routed command to sort the column. /// </summary> /// <param name="sender">The Object that generated the event.</param> /// <param name="routedEventArgs">The routed event arguments.</param> void OnHeaderClick(Object sender, RoutedEventArgs routedEventArgs) { // Extract from the command arguments the column header that generated a request to be autosized and then resize the column. ColumnViewColumnHeader columnViewColumnHeader = routedEventArgs.OriginalSource as ColumnViewColumnHeader; ColumnViewColumn columnViewColumn = columnViewColumnHeader.Column; // This will prevent each change we make to the view from triggering an update of the viewed data. using (this.consumerCollection.View.DeferRefresh()) { // Clear out the existing sort order from the view. this.consumerCollection.View.SortDescriptions.Clear(); // Clear the sort order for the other columns. foreach (ColumnViewColumn siblingColumn in this.ColumnView.Columns) { if (siblingColumn != columnViewColumn) { siblingColumn.SortDirection = SortDirection.None; } } // If a DisplayMemberBinding exists for this column, we'll use that binding to find the property used to populate this column. Binding binding = columnViewColumn.DisplayMemberBinding as Binding; if (binding != null) { // This will toggle the sort order for this column (or select Ascending if the sort order hasn't been set) and set the corresonding sort order in // the collection view. columnViewColumn.SortDirection = columnViewColumn.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending; ListSortDirection listSortDirection = columnViewColumn.SortDirection == SortDirection.Descending ? ListSortDirection.Descending : ListSortDirection.Ascending; this.consumerCollection.View.SortDescriptions.Add(new SortDescription(binding.Path.Path, listSortDirection)); } } }