/// <summary> /// Reporting a key was pressed. /// </summary> protected override void OnKeyDown(KeyEventArgs e) { if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && (e.Key == Key.Left || e.Key == Key.Right)) { DataGridLength updatedWidth = new DataGridLength(); if (e.Key == Key.Right) { updatedWidth = new DataGridLength(this.Column.ActualWidth + ColumnWidthStepSize); } else if (e.Key == Key.Left) { updatedWidth = new DataGridLength(this.Column.ActualWidth - ColumnWidthStepSize); } if (Column.CanColumnResize(updatedWidth)) { this.Column.SetCurrentValue(DataGridColumn.WidthProperty, updatedWidth); } e.Handled = true; return; } SendInputToColumn(e); }
/// <summary> /// Attempts to convert a DataGridLength instance to the given type. /// </summary> /// <param name="context">The ITypeDescriptorContext for this call.</param> /// <param name="culture">The CultureInfo which is respected when converting.</param> /// <param name="value">The DataGridLength to convert.</param> /// <param name="destinationType">The type to which to convert the DataGridLength instance.</param> /// <returns> /// The object which was constructed. /// </returns> /// <exception cref="ArgumentNullException"> /// An ArgumentNullException is thrown if the value is null. /// </exception> /// <exception cref="ArgumentException"> /// An ArgumentException is thrown if the value is not null and is not a DataGridLength, /// or if the destinationType isn't one of the valid destination types. /// </exception> public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if ((value != null) && (value is DataGridLength)) { DataGridLength length = (DataGridLength)value; if (destinationType == typeof(string)) { return(ConvertToString(length, culture)); } if (destinationType == typeof(InstanceDescriptor)) { ConstructorInfo ci = typeof(DataGridLength).GetConstructor(new Type[] { typeof(double), typeof(DataGridLengthUnitType) }); return(new InstanceDescriptor(ci, new object[] { length.Value, length.UnitType })); } } // The default exception to throw from ConvertTo throw GetConvertToException(value, destinationType); }
// Token: 0x060048D3 RID: 18643 RVA: 0x0014A864 File Offset: 0x00148A64 public static void OnColumnWidthChanged(IProvideDataGridColumn cell, DependencyPropertyChangedEventArgs e) { UIElement uielement = (UIElement)cell; DataGridColumn column = cell.Column; bool flag = cell is DataGridColumnHeader; if (column != null) { DataGridLength width = column.Width; if (width.IsAuto || (!flag && width.IsSizeToCells) || (flag && width.IsSizeToHeader)) { DataGridLength dataGridLength = (DataGridLength)e.OldValue; double num; if (dataGridLength.UnitType != width.UnitType) { double constraintWidth = column.GetConstraintWidth(flag); if (!DoubleUtil.AreClose(uielement.DesiredSize.Width, constraintWidth)) { uielement.InvalidateMeasure(); uielement.Measure(new Size(constraintWidth, double.PositiveInfinity)); } num = uielement.DesiredSize.Width; } else { num = dataGridLength.DesiredValue; } if (DoubleUtil.IsNaN(width.DesiredValue) || DoubleUtil.LessThan(width.DesiredValue, num)) { column.SetWidthInternal(new DataGridLength(width.Value, width.UnitType, num, width.DisplayValue)); } } } }
/// <summary> /// Coerces a DataGridLength to a valid value. If any value components are double.NaN, this method /// coerces them to a proper initial value. For star columns, the desired width is calculated based /// on the rest of the star columns. For pixel widths, the desired value is based on the pixel value. /// For auto widths, the desired value is initialized as the column's minimum width. /// </summary> /// <param name="width">The DataGridLength to coerce.</param> /// <returns>The resultant (coerced) DataGridLength.</returns> internal DataGridLength CoerceWidth(DataGridLength width) { double desiredValue = width.DesiredValue; if (double.IsNaN(desiredValue)) { if (width.IsStar && this.OwningGrid != null && this.OwningGrid.ColumnsInternal != null) { double totalStarValues = 0; double totalStarDesiredValues = 0; double totalNonStarDisplayWidths = 0; foreach (DataGridColumn column in this.OwningGrid.ColumnsInternal.GetDisplayedColumns(c => c.IsVisible && c != this && !double.IsNaN(c.Width.DesiredValue))) { if (column.Width.IsStar) { totalStarValues += column.Width.Value; totalStarDesiredValues += column.Width.DesiredValue; } else { totalNonStarDisplayWidths += column.ActualWidth; } } if (totalStarValues == 0) { // Compute the new star column's desired value based on the available space if there are no other visible star columns desiredValue = Math.Max(this.ActualMinWidth, this.OwningGrid.CellsWidth - totalNonStarDisplayWidths); } else { // Otherwise, compute its desired value based on those of other visible star columns desiredValue = totalStarDesiredValues * width.Value / totalStarValues; } } else if (width.IsAbsolute) { desiredValue = width.Value; } else { desiredValue = this.ActualMinWidth; } } double displayValue = width.DisplayValue; if (double.IsNaN(displayValue)) { displayValue = desiredValue; } displayValue = Math.Max(this.ActualMinWidth, Math.Min(this.ActualMaxWidth, displayValue)); return(new DataGridLength(width.Value, width.UnitType, desiredValue, displayValue)); }
/// <summary> /// Set the column's Width without breaking inheritance. /// </summary> /// <param name="width">The new Width.</param> internal void SetWidthInternal(DataGridLength width) { bool originalValue = _settingWidthInternally; _settingWidthInternally = true; try { this.Width = width; } finally { _settingWidthInternally = originalValue; } }
/// <summary> /// Converts a DataGridLength instance to a String given the CultureInfo. /// </summary> /// <param name="gl">DataGridLength instance to convert.</param> /// <param name="cultureInfo">The culture to use.</param> /// <returns>String representation of the object.</returns> internal static string ConvertToString(DataGridLength length, CultureInfo cultureInfo) { switch (length.UnitType) { case DataGridLengthUnitType.Auto: case DataGridLengthUnitType.SizeToCells: case DataGridLengthUnitType.SizeToHeader: return(length.UnitType.ToString()); // Star has one special case when value is "1.0" in which the value can be dropped. case DataGridLengthUnitType.Star: return(DoubleUtil.IsOne(length.Value) ? "*" : Convert.ToString(length.Value, cultureInfo) + "*"); // Print out the numeric value. "px" can be omitted. default: return(Convert.ToString(length.Value, cultureInfo)); } }
// Token: 0x06004920 RID: 18720 RVA: 0x0014B824 File Offset: 0x00149A24 internal static string ConvertToString(DataGridLength length, CultureInfo cultureInfo) { switch (length.UnitType) { case DataGridLengthUnitType.Auto: case DataGridLengthUnitType.SizeToCells: case DataGridLengthUnitType.SizeToHeader: return(length.UnitType.ToString()); case DataGridLengthUnitType.Star: if (!DoubleUtil.IsOne(length.Value)) { return(Convert.ToString(length.Value, cultureInfo) + "*"); } return("*"); } return(Convert.ToString(length.Value, cultureInfo)); }
/// <summary> /// Invalidates a cell's panel if its column's width changes sufficiently. /// </summary> /// <param name="cell">The cell or header.</param> /// <param name="e"></param> public static void OnColumnWidthChanged(IProvideDataGridColumn cell, DependencyPropertyChangedEventArgs e) { Debug.Assert((cell is DataGridCell) || (cell is DataGridColumnHeader), "provideColumn should be one of the cell or header containers."); UIElement element = (UIElement)cell; DataGridColumn column = cell.Column; bool isColumnHeader = (cell is DataGridColumnHeader); if (column != null) { // determine the desired value of width for auto kind columns DataGridLength width = column.Width; if (width.IsAuto || (!isColumnHeader && width.IsSizeToCells) || (isColumnHeader && width.IsSizeToHeader)) { DataGridLength oldWidth = (DataGridLength)e.OldValue; double desiredWidth = 0.0; if (oldWidth.UnitType != width.UnitType) { double constraintWidth = column.GetConstraintWidth(isColumnHeader); if (!DoubleUtil.AreClose(element.DesiredSize.Width, constraintWidth)) { element.InvalidateMeasure(); element.Measure(new Size(constraintWidth, double.PositiveInfinity)); } desiredWidth = element.DesiredSize.Width; } else { desiredWidth = oldWidth.DesiredValue; } if (DoubleUtil.IsNaN(width.DesiredValue) || DoubleUtil.LessThan(width.DesiredValue, desiredWidth)) { column.SetWidthInternal(new DataGridLength(width.Value, width.UnitType, desiredWidth, width.DisplayValue)); } } } }
/// <summary> /// Sets the column's Width directly, without any callback effects. /// </summary> /// <param name="width">The new Width.</param> internal void SetWidthInternalNoCallback(DataGridLength width) { _width = width; }
/// <summary> /// Attempts to resize the column's width to the desired DisplayValue, but limits the final size /// to the column's minimum and maximum values. If star sizing is being used, then the column /// can only decrease in size by the amount that the columns after it can increase in size. /// Likewise, the column can only increase in size if other columns can spare the width. /// </summary> /// <param name="value">The new Value.</param> /// <param name="unitType">The new UnitType.</param> /// <param name="desiredValue">The new DesiredValue.</param> /// <param name="displayValue">The new DisplayValue.</param> /// <param name="userInitiated">Whether or not this resize was initiated by a user action.</param> internal void Resize(double value, DataGridLengthUnitType unitType, double desiredValue, double displayValue, bool userInitiated) { Debug.Assert(this.OwningGrid != null); double newValue = value; double newDesiredValue = desiredValue; double newDisplayValue = Math.Max(this.ActualMinWidth, Math.Min(this.ActualMaxWidth, displayValue)); DataGridLengthUnitType newUnitType = unitType; int starColumnsCount = 0; double totalDisplayWidth = 0; foreach (DataGridColumn column in this.OwningGrid.ColumnsInternal.GetVisibleColumns()) { column.EnsureWidth(); totalDisplayWidth += column.ActualWidth; starColumnsCount += (column != this && column.Width.IsStar) ? 1 : 0; } bool hasInfiniteAvailableWidth = !this.OwningGrid.RowsPresenterAvailableSize.HasValue || double.IsPositiveInfinity(this.OwningGrid.RowsPresenterAvailableSize.Value.Width); // If we're using star sizing, we can only resize the column as much as the columns to the // right will allow (i.e. until they hit their max or min widths). if (!hasInfiniteAvailableWidth && (starColumnsCount > 0 || (unitType == DataGridLengthUnitType.Star && this.Width.IsStar && userInitiated))) { double limitedDisplayValue = this.Width.DisplayValue; double availableIncrease = Math.Max(0, this.OwningGrid.CellsWidth - totalDisplayWidth); double desiredChange = newDisplayValue - this.Width.DisplayValue; if (desiredChange > availableIncrease) { // The desired change is greater than the amount of available space, // so we need to decrease the widths of columns to the right to make room. desiredChange -= availableIncrease; double actualChange = desiredChange + this.OwningGrid.DecreaseColumnWidths(this.DisplayIndex + 1, -desiredChange, userInitiated); limitedDisplayValue += availableIncrease + actualChange; } else if (desiredChange > 0) { // The desired change is positive but less than the amount of available space, // so there's no need to decrease the widths of columns to the right. limitedDisplayValue += desiredChange; } else { // The desired change is negative, so we need to increase the widths of columns to the right. limitedDisplayValue += desiredChange + this.OwningGrid.IncreaseColumnWidths(this.DisplayIndex + 1, -desiredChange, userInitiated); } if (this.ActualCanUserResize || (this.Width.IsStar && !userInitiated)) { newDisplayValue = limitedDisplayValue; } } if (userInitiated) { newDesiredValue = newDisplayValue; if (!this.Width.IsStar) { this.InheritsWidth = false; newValue = newDisplayValue; newUnitType = DataGridLengthUnitType.Pixel; } else if (starColumnsCount > 0 && !hasInfiniteAvailableWidth) { // Recalculate star weight of this column based on the new desired value this.InheritsWidth = false; newValue = (this.Width.Value * newDisplayValue) / this.ActualWidth; } } DataGridLength oldWidth = this.Width; SetWidthInternalNoCallback(new DataGridLength(Math.Min(double.MaxValue, newValue), newUnitType, newDesiredValue, newDisplayValue)); if (this.Width != oldWidth) { this.OwningGrid.OnColumnWidthChanged(this); } }