{"record":{"id":"2218d28b83b187dc","repo":"dotnet/wpf","slug":"sr-format-sr-lengthformaterror-span-tostring","errorCode":null,"errorMessage":"SR.Format(SR.LengthFormatError, span.ToString())","messagePattern":"SR\\.Format\\(SR\\.LengthFormatError, span\\.ToString\\(\\)\\)","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/LengthConverter.cs","lineNumber":225,"sourceCode":"\n            if (valueSpan.IsEmpty)\n                return 0;\n\n            return ParseDouble(valueSpan, cultureInfo) * unitFactor;\n        }\n\n        private static double ParseDouble(ReadOnlySpan<char> span, CultureInfo cultureInfo)\n        {\n            // FormatException errors thrown by double.Parse are pretty uninformative.\n            // Throw a more meaningful error in this case that tells that we were attempting\n            // to create a Length instance from a string.  This addresses windows bug 968884\n            try\n            {\n                return double.Parse(span, cultureInfo);\n            }\n            catch (FormatException)\n            {\n                throw new FormatException(SR.Format(SR.LengthFormatError, span.ToString()));\n            }\n        }\n\n\n        #endregion\n\n    }\n}\n","sourceCodeStart":207,"sourceCodeEnd":234,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/LengthConverter.cs#L207-L234","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nbutton.Width = (double)LengthConverter.FromString(\"12px\", culture);\n// after\nbutton.Width = (double)LengthConverter.FromString(\"12\", culture);\n// or for Auto:\nbutton.Width = double.NaN;","handlingStrategy":"try-catch","validationCode":"bool IsValidLength(string s) =>\n    double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out _);","typeGuard":"bool TryGetLength(string s, out double value) =>\n    double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value);","tryCatchPattern":"try\n{\n    value = (double)LengthConverter.FromString(input, culture);\n}\ncatch (FormatException ex)\n{\n    Log($\"'{input}' is not a valid WPF length: {ex.Message}\");\n    value = double.NaN; // Auto\n}","preventionTips":["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."],"tags":["wpf","xaml","format","parsing"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}