dotnet/wpf · error · ArgumentException

Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…

Error message

Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidCtorParameterNoNaN, "value")

What it means

The RibbonControlLength(double, RibbonControlLengthUnitType) constructor rejects NaN values because a length cannot be 'not a number'; it throws ArgumentException with a message saying the 'value' parameter must not be NaN. (Star units additionally reject infinity, but the NaN check applies to all unit types.)

Solutions

  1. Guard the input with double.IsNaN(value) before constructing and substitute a sane default (e.g. Auto or a fixed pixel length).
  2. Delay construction until layout has produced real measurements instead of using ActualWidth during measure/arrange pass.
  3. Fix the upstream calculation producing NaN (check for division by zero or uninitialized doubles).
  4. If the API surface allows, use RibbonControlLength.Auto instead of a NaN-sized value.

Example fix

// before
var length = new RibbonControlLength(element.ActualWidth, RibbonControlLengthUnitType.Pixel); // NaN before layout

// after
var length = double.IsNaN(element.ActualWidth)
    ? RibbonControlLength.Auto
    : new RibbonControlLength(element.ActualWidth, RibbonControlLengthUnitType.Pixel);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(raw))
    throw new ArgumentException("Length value must not be NaN before constructing RibbonControlLength");
var length = new RibbonControlLength(raw, RibbonControlLengthUnitType.Pixel);

Type guard

bool IsUsableLength(double v) => !double.IsNaN(v) && !(type == RibbonControlLengthUnitType.Star && double.IsInfinity(v)); // pair with the unit type in scope

Try / catch

try { var length = new RibbonControlLength(value, type); }
catch (ArgumentException) { var length = RibbonControlLength.Auto; /* NaN from unfinished layout */ }

Prevention

When it happens

Trigger: Calling new RibbonControlLength(double.NaN, ...) with any unit type - typically when a computed width/height (e.g. from ActualWidth during layout, or an unbound/databound double that defaulted to NaN) is passed to the constructor.

Common situations: Passing a control's ActualWidth before layout completes (NaN), databinding a length property where the source is NaN, or math that produced NaN (0/0, infinity-infinity) before constructing the length.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonControlLength.cs:50

        #region Constructors

        /// <summary>
        ///   Constructor, initializes the RibbonControlLength as absolute value in pixels.
        /// </summary>
        public RibbonControlLength(double pixels)
            : this(pixels, RibbonControlLengthUnitType.Pixel)
        {
        }

        /// <summary>
        ///   Constructor, initializes the RibbonControlLength and specifies what kind of value 
        ///   it will hold.
        /// </summary>
        public RibbonControlLength(double value, RibbonControlLengthUnitType type)
        {
            if (double.IsNaN(value))
            {
                throw new ArgumentException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidCtorParameterNoNaN, "value"));
            }

            if (type == RibbonControlLengthUnitType.Star && double.IsInfinity(value))
            {
                throw new ArgumentException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidCtorParameterNoInfinityForStarSize, "value"));
            }

            if (type != RibbonControlLengthUnitType.Auto
                && type != RibbonControlLengthUnitType.Pixel
                && type != RibbonControlLengthUnitType.Item
                && type != RibbonControlLengthUnitType.Star)
            {
                throw new ArgumentException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidCtorParameterUnknownRibbonControlLengthUnitType, "type"));
            }

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

View on GitHub (pinned to 81131a70a4)