dotnet/wpf · error · ArgumentException
SR.Format(SR.InvalidCtorParameterUnknownGridUnitType…
Error message
SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, "type")
What it means
GridLength's constructor only accepts GridUnitType.Auto, GridUnitType.Pixel, or GridUnitType.Star. Any other GridUnitType value (including undefined numeric casts) fails this guard and throws ArgumentException (SR.InvalidCtorParameterUnknownGridUnitType) because the unit type would have no meaning for layout.
Solutions
- Validate the unit type before constructing: Enum.IsDefined(typeof(GridUnitType), type) and that it is one of Auto/Pixel/Star.
- Map unknown values to a safe default (Pixel or Auto) at the deserialization boundary.
- Persist GridUnitType as its named string rather than a raw integer.
- Audit cast sites: replace (GridUnitType)intValue with an explicit switch that has a default fallback.
Example fix
// before var length = new GridLength(value, (GridUnitType)storedInt); // after var type = storedInt is >= 0 and <= 2 ? (GridUnitType)storedInt : GridUnitType.Pixel; var length = new GridLength(type == GridUnitType.Auto ? 0 : value, type);
Defensive patterns
Strategy: validation
Validate before calling
if (type is not (GridUnitType.Auto or GridUnitType.Pixel or GridUnitType.Star))
type = GridUnitType.Pixel;
var length = new GridLength(value, type); Type guard
bool IsValidGridUnitType(GridUnitType t) => t is GridUnitType.Auto or GridUnitType.Pixel or GridUnitType.Star;
Try / catch
try { len = new GridLength(value, type); }
catch (ArgumentException ex) when (ex.ParamName == "type")
{ len = new GridLength(value, GridUnitType.Pixel); } Prevention
- Persist GridUnitType as a string, not an int.
- Never cast raw ints to GridUnitType without Enum.IsDefined.
- Restrict UI inputs to the three valid unit types via a whitelist.
When it happens
Trigger: new GridLength(10, (GridUnitType)42), deserializing a persisted enum int that is not 0/1/2, or passing a GridUnitType member added/renamed in another assembly version.
Common situations: Round-tripping GridLength settings through config or XAML-adjacent serialization where the enum was stored as a raw int; interop with code that uses different grid-unit constants; enum arithmetic producing out-of-range values.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- InvalidValueOfType
- SR.Format(SR.InvalidCtorParameterNoInfinity, "value")
- Animation_UnrecognizedHandoffBehavior
- args
- ArgumentOutOfRangeException(authentication)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/089a8705b16b8dc8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs:102
/// 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
//------------------------------------------------------
//
// Public Methods
//
//------------------------------------------------------
#region Public Methods
/// <summary>
/// Overloaded operator, compares 2 GridLength's.View on GitHub (pinned to 81131a70a4)