dotnet/wpf · error · FormatException
SR.Format(SR.LengthFormatError, span.ToString())
Error message
SR.Format(SR.LengthFormatError, span.ToString())
What it means
LengthConverter.ParseDouble (used by LengthConverter.FromString) wraps a failed double.Parse of a XAML/converter length string in a FormatException with the LengthFormatError message including the offending span. WPF length values must be parseable numbers (optionally with units like 'in', 'cm', 'pt' handled by the converter); a malformed numeric string triggers this.
Solutions
- Correct the string to a valid numeric length (e.g. '12' or '12.5'), or use CSS-like units LengthConverter accepts ('in','cm','pt').
- Use Double.NaN for 'Auto' instead of the text 'auto'.
- In code, parse with the correct CultureInfo (CultureInfo.InvariantCulture) before assigning.
- Wrap FromString/parse calls in try-catch on FormatException and surface a friendly message.
Example fix
// before
button.Width = (double)LengthConverter.FromString("12px", culture);
// after
button.Width = (double)LengthConverter.FromString("12", culture);
// or for Auto:
button.Width = double.NaN; Defensive patterns
Strategy: try-catch
Validate before calling
bool IsValidLength(string s) =>
double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out _); Type guard
bool TryGetLength(string s, out double value) =>
double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value); Try / catch
try
{
value = (double)LengthConverter.FromString(input, culture);
}
catch (FormatException ex)
{
Log($"'{input}' is not a valid WPF length: {ex.Message}");
value = double.NaN; // Auto
} Prevention
- Use invariant culture or the converter's expected format when parsing lengths in code.
- Use double.NaN (not 'auto') for Auto sizes.
- Never copy CSS units like 'px' into XAML length attributes.
- Validate length strings before assigning to dependency properties via converters.
When it happens
Trigger: Setting a length-typed dependency property (Width, Height, etc.) via XAML or a string converter with a value like '12px', 'auto', '1,5' with wrong culture, or any non-numeric text that double.Parse rejects.
Common situations: Typo'd XAML attribute values, culture mismatches (decimal comma vs dot when converting strings in code), CSS-style units copied into XAML, and empty or whitespace strings.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- FormatException(error from XamlTypeName.Parse)
- FormatException(error from XamlTypeName.ParseList)
- MappingParseError(_scanner.Start, MappingScanner.Ident…
- SR.CompositeFontInvalidUnicodeRange
- SR.Format(SR.InvalidStringCornerRadius, s)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2218d28b83b187dc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/LengthConverter.cs:225
if (valueSpan.IsEmpty)
return 0;
return ParseDouble(valueSpan, cultureInfo) * unitFactor;
}
private static double ParseDouble(ReadOnlySpan<char> span, CultureInfo cultureInfo)
{
// FormatException errors thrown by double.Parse are pretty uninformative.
// Throw a more meaningful error in this case that tells that we were attempting
// to create a Length instance from a string. This addresses windows bug 968884
try
{
return double.Parse(span, cultureInfo);
}
catch (FormatException)
{
throw new FormatException(SR.Format(SR.LengthFormatError, span.ToString()));
}
}
#endregion
}
}
View on GitHub (pinned to 81131a70a4)