protected override Size MeasureOverride(Size availableSize) { if (this.OwningGrid == null) { return(base.MeasureOverride(availableSize)); } if (!this.OwningGrid.AreColumnHeadersVisible) { return(Size.Empty); } double height = this.OwningGrid.ColumnHeaderHeight; bool autoSizeHeight; if (double.IsNaN(height)) { // No explicit height values were set so we can autosize height = 0; autoSizeHeight = true; } else { autoSizeHeight = false; } DataGridColumn lastVisibleColumn = this.OwningGrid.ColumnsInternal.LastVisibleColumn; DataGridColumn column = this.OwningGrid.ColumnsInternal.FirstVisibleColumn; double measureWidth; while (column != null) { DataGridLength columnWidth = column.EffectiveWidth; measureWidth = columnWidth.IsAbsolute ? column.ActualWidth : column.ActualMaxWidth; bool autoGrowWidth = columnWidth.IsAuto || columnWidth.IsSizeToHeader; DataGridColumnHeader columnHeader = column.HeaderCell; if (column != lastVisibleColumn) { columnHeader.UpdateSeparatorVisibility(lastVisibleColumn); } columnHeader.Measure(new Size(measureWidth, double.PositiveInfinity)); if (autoSizeHeight) { height = Math.Max(height, columnHeader.DesiredSize.Height); } if (autoGrowWidth && columnHeader.DesiredSize.Width > column.DesiredWidth) { column.DesiredWidth = columnHeader.DesiredSize.Width; } column = this.OwningGrid.ColumnsInternal.GetNextVisibleColumn(column); } // Add the filler column if it's not represented. We won't know whether we need it or not until Arrange DataGridFillerColumn fillerColumn = this.OwningGrid.ColumnsInternal.FillerColumn; if (!fillerColumn.IsRepresented) { Debug.Assert(!this.Children.Contains(fillerColumn.HeaderCell)); fillerColumn.HeaderCell.SeparatorVisibility = Visibility.Collapsed; this.Children.Insert(this.OwningGrid.ColumnsInternal.Count, fillerColumn.HeaderCell); fillerColumn.IsRepresented = true; this.OwningGrid.OnFillerColumnRepresentationChanged(); // Optimize for the case where we don't need the filler cell fillerColumn.HeaderCell.Visibility = Visibility.Collapsed; } fillerColumn.HeaderCell.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return(new Size(this.OwningGrid.ColumnsInternal.VisibleEdgedColumnsWidth, height)); }
/// <summary> /// Measures the children of a <see cref="T:System.Windows.Controls.Primitives.DataGridColumnHeadersPresenter" /> to /// prepare for arranging them during the <see cref="M:System.Windows.FrameworkElement.ArrangeOverride(System.Windows.Size)" /> pass. /// </summary> /// <param name="availableSize"> /// The available size that this element can give to child elements. Indicates an upper limit that child elements should not exceed. /// </param> /// <returns> /// The size that the <see cref="T:System.Windows.Controls.Primitives.DataGridColumnHeadersPresenter" /> determines it needs during layout, based on its calculations of child object allocated sizes. /// </returns> protected override Size MeasureOverride(Size availableSize) { if (this.OwningGrid == null) { return(base.MeasureOverride(availableSize)); } if (!this.OwningGrid.AreColumnHeadersVisible) { return(Size.Empty); } double height = this.OwningGrid.ColumnHeaderHeight; bool autoSizeHeight; if (double.IsNaN(height)) { // No explicit height values were set so we can autosize height = 0; autoSizeHeight = true; } else { autoSizeHeight = false; } double totalDisplayWidth = 0; this.OwningGrid.ColumnsInternal.EnsureVisibleEdgedColumnsWidth(); DataGridColumn lastVisibleColumn = this.OwningGrid.ColumnsInternal.LastVisibleColumn; foreach (DataGridColumn column in this.OwningGrid.ColumnsInternal.GetVisibleColumns()) { // Measure each column header bool autoGrowWidth = column.Width.IsAuto || column.Width.IsSizeToHeader; DataGridColumnHeader columnHeader = column.HeaderCell; if (column != lastVisibleColumn) { columnHeader.UpdateSeparatorVisibility(lastVisibleColumn); } // If we're not using star sizing or the current column can't be resized, // then just set the display width according to the column's desired width if (!this.OwningGrid.UsesStarSizing || (!column.ActualCanUserResize && !column.Width.IsStar)) { // In the edge-case where we're given infinite width and we have star columns, the // star columns grow to their predefined limit of 10,000 (or their MaxWidth) double newDisplayWidth = column.Width.IsStar ? Math.Min(column.ActualMaxWidth, DataGrid.DATAGRID_maximumStarColumnWidth) : Math.Max(column.ActualMinWidth, Math.Min(column.ActualMaxWidth, column.Width.DesiredValue)); column.SetWidthDisplayValue(newDisplayWidth); } // If we're auto-growing the column based on the header content, we want to measure it at its maximum value if (autoGrowWidth) { columnHeader.Measure(new Size(column.ActualMaxWidth, double.PositiveInfinity)); this.OwningGrid.AutoSizeColumn(column, columnHeader.DesiredSize.Width); column.ComputeLayoutRoundedWidth(totalDisplayWidth); } else if (!this.OwningGrid.UsesStarSizing) { column.ComputeLayoutRoundedWidth(totalDisplayWidth); columnHeader.Measure(new Size(column.LayoutRoundedWidth, double.PositiveInfinity)); } // We need to track the largest height in order to auto-size if (autoSizeHeight) { height = Math.Max(height, columnHeader.DesiredSize.Height); } totalDisplayWidth += column.ActualWidth; } // If we're using star sizing (and we're not waiting for an auto-column to finish growing) // then we will resize all the columns to fit the available space. if (this.OwningGrid.UsesStarSizing && !this.OwningGrid.AutoSizingColumns) { double adjustment = Double.IsPositiveInfinity(availableSize.Width) ? this.OwningGrid.CellsWidth : availableSize.Width - totalDisplayWidth; totalDisplayWidth += adjustment - this.OwningGrid.AdjustColumnWidths(0, adjustment, false); // Since we didn't know the final widths of the columns until we resized, // we waited until now to measure each header double leftEdge = 0; foreach (DataGridColumn column in this.OwningGrid.ColumnsInternal.GetVisibleColumns()) { column.ComputeLayoutRoundedWidth(leftEdge); column.HeaderCell.Measure(new Size(column.LayoutRoundedWidth, double.PositiveInfinity)); if (autoSizeHeight) { height = Math.Max(height, column.HeaderCell.DesiredSize.Height); } leftEdge += column.ActualWidth; } } // Add the filler column if it's not represented. We won't know whether we need it or not until Arrange DataGridFillerColumn fillerColumn = this.OwningGrid.ColumnsInternal.FillerColumn; if (!fillerColumn.IsRepresented) { Debug.Assert(!this.Children.Contains(fillerColumn.HeaderCell)); fillerColumn.HeaderCell.SeparatorVisibility = Visibility.Collapsed; this.Children.Insert(this.OwningGrid.ColumnsInternal.Count, fillerColumn.HeaderCell); fillerColumn.IsRepresented = true; // Optimize for the case where we don't need the filler cell fillerColumn.HeaderCell.Visibility = Visibility.Collapsed; } fillerColumn.HeaderCell.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); if (this.DragIndicator != null) { this.DragIndicator.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); } if (this.DropLocationIndicator != null) { this.DropLocationIndicator.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); } this.OwningGrid.ColumnsInternal.EnsureVisibleEdgedColumnsWidth(); return(new Size(this.OwningGrid.ColumnsInternal.VisibleEdgedColumnsWidth, height)); }