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);
        }
        /// <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));
            }
        }
示例#3
0
        /// <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));
                    }
                }
            }
        }