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
- Guard the input with double.IsNaN(value) before constructing and substitute a sane default (e.g. Auto or a fixed pixel length).
- Delay construction until layout has produced real measurements instead of using ActualWidth during measure/arrange pass.
- Fix the upstream calculation producing NaN (check for division by zero or uninitialized doubles).
- 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
- Never pass ActualWidth/ActualHeight during the measure pass; they can be NaN.
- Validate doubles for NaN (and Infinity for Star units) before constructing lengths.
- Prefer RibbonControlLength.Auto over a computed NaN value.
- Fix upstream math that can yield NaN (division by zero, inf-inf).
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
- InvalidMatrixContainsNaN
- Microsoft.Windows.Controls.SR.InvalidKeyTipOffset
- Microsoft.Windows.Controls.SR.RibbonGroupsPanel_InvalidRegis…
- SR.Format(SR.InvalidCtorParameterNoInfinity, "value")
- SR.Format(SR.InvalidCtorParameterNoNaN…
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)