dotnet/wpf · error · ArgumentException

SR.DataGridLength_Infinity

Error message

SR.DataGridLength_Infinity

What it means

The DataGridLength(double value, DataGridLengthUnitType type, ...) constructor throws ArgumentException (message SR.DataGridLength_Infinity) when the value parameter is NaN or infinite. DataGridLength only accepts finite pixel/unit values; special sizes (Auto, Star, SizeToCells, SizeToHeader) are expressed via the type, not via NaN/Infinity.

Solutions

  1. Use DataGridLength.Auto or DataGridLengthUnitType.Auto instead of NaN
  2. Pass a finite positive double for value
  3. Guard the input with double.IsNaN/IsInfinity before constructing
  4. Use DataGridLength.SizeToHeader/SizeToCells for content-based sizing

Example fix

// before
column.Width = new DataGridLength(double.NaN);
// after
column.Width = DataGridLength.Auto;
Defensive patterns

Strategy: validation

Validate before calling

bool isValidLength(double v) => !double.IsNaN(v) && !double.IsInfinity(v);

Type guard

bool TryGetDataGridLength(double v, out DataGridLength len) { len = isValidLength(v) ? new DataGridLength(v) : DataGridLength.Auto; return isValidLength(v); }

Try / catch

try { len = new DataGridLength(value); } catch (ArgumentException) { len = DataGridLength.Auto; }

Prevention

When it happens

Trigger: new DataGridLength(double.NaN), new DataGridLength(double.PositiveInfinity), or passing NaN/Infinity as value while constructing a length for a DataGridColumn.Width.

Common situations: Porting GridLength code where 'Auto' was represented as double.NaN; binding a Width value from data that contains NaN; division producing Infinity before constructing the length.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bebf9e2dd936aeaa. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridLength.cs:77

        /// <param name="value">The value to hold.</param>
        /// <param name="type">The unit of <c>value</c>.</param>
        /// <param name="desiredValue"></param>
        /// <param name="displayValue"></param>
        /// <remarks> 
        ///     <c>value</c> is ignored unless <c>type</c> is
        ///     <c>DataGridLengthUnitType.Pixel</c> or
        ///     <c>DataGridLengthUnitType.Star</c>
        /// </remarks>
        /// <exception cref="ArgumentException">
        ///     If <c>value</c> parameter is <c>double.NaN</c>
        ///     or <c>value</c> parameter is <c>double.NegativeInfinity</c>
        ///     or <c>value</c> parameter is <c>double.PositiveInfinity</c>.
        /// </exception>
        public DataGridLength(double value, DataGridLengthUnitType type, double desiredValue, double displayValue)
        {
            if (double.IsNaN(value) || Double.IsInfinity(value))
            {
                throw new ArgumentException(
                    SR.DataGridLength_Infinity,
                    nameof(value));
            }

            if (type != DataGridLengthUnitType.Auto &&
                type != DataGridLengthUnitType.Pixel &&
                type != DataGridLengthUnitType.Star &&
                type != DataGridLengthUnitType.SizeToCells &&
                type != DataGridLengthUnitType.SizeToHeader)
            {
                throw new ArgumentException(
                    SR.DataGridLength_InvalidType, 
                    nameof(type));
            }

            if (Double.IsInfinity(desiredValue))
            {
                throw new ArgumentException(

View on GitHub (pinned to 81131a70a4)