dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidCtorParameterNoNaN, "value")

Error message

SR.Format(SR.InvalidCtorParameterNoNaN, "value")

What it means

The GridLength(double value, GridUnitType type) constructor validates that 'value' is a finite, non-NaN number. NaN is rejected with ArgumentException (InvalidCtorParameterNoNaN); infinity is rejected with InvalidCtorParameterNoInfinity; and only Auto (where the value is ignored) plus valid unit types are allowed.

Solutions

  1. Validate the value before constructing: use double.IsNaN/IsInfinity checks and substitute a sensible default (e.g. GridLength.Auto or a fixed pixel size).
  2. Use predefined lengths where possible: GridLength.Auto, new GridLength(1, GridUnitType.Star) instead of raw doubles.
  3. Fix the upstream source producing NaN (e.g. empty parsed value or failed measurement) rather than defaulting at the call site.

Example fix

// before
var len = new GridLength(parsedValue, GridUnitType.Pixel); // ArgumentException if parsedValue is NaN

// after
var len = double.IsNaN(parsedValue) || double.IsInfinity(parsedValue)
    ? GridLength.Auto
    : new GridLength(parsedValue, GridUnitType.Pixel);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(value) || double.IsInfinity(value))
    throw new ArgumentException($"GridLength value must be finite, got {value}.");
var len = new GridLength(value, type);

Type guard

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

Try / catch

try
{
    var len = new GridLength(value, GridUnitType.Pixel);
}
catch (ArgumentException ex) when (ex.Message.Contains("NaN") || ex.Message.Contains("value"))
{
    Log.Warn($"Invalid GridLength value {value}; falling back to Auto.", ex);
}

Prevention

When it happens

Trigger: Calling new GridLength(double.NaN, ...) (NaN passed as the length value); the exception is thrown before the type checks, so any GridUnitType with a NaN value fails — except a NaN still throws even for GridUnitType.Auto.

Common situations: Passing an uncomputed double (result of 0.0/0.0, NaN from a binding or calculation) into the constructor; deserializing grid lengths from data where missing values become NaN; programmatic grid column/row definitions built from parsed or measured values.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs:92

        /// </summary>
        /// <param name="value">Value to be stored by this GridLength 
        /// instance.</param>
        /// <param name="type">Type of the value to be stored by this GridLength 
        /// instance.</param>
        /// <remarks> 
        /// If the <c>type</c> parameter is <c>GridUnitType.Auto</c>, 
        /// then passed in value is ignored and replaced with <c>0</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 GridLength(double value, GridUnitType type)
        {
            if (double.IsNaN(value))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, "value"));
            }
            if (double.IsInfinity(value))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoInfinity, "value"));
            }
            if (    type != GridUnitType.Auto
                &&  type != GridUnitType.Pixel
                &&  type != GridUnitType.Star   )
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, "type"));
            }

            _unitValue = (type == GridUnitType.Auto) ? 0.0 : value;
            _unitType = type;
        }

        #endregion Constructors

View on GitHub (pinned to 81131a70a4)