{"record":{"id":"71918c3bba03da61","repo":"AvaloniaUI/Avalonia","slug":"if-one-coordinate-is-relative-all-must-be","errorCode":null,"errorMessage":"If one coordinate is relative, all must be.","messagePattern":"If one coordinate is relative, all must be\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/RelativeRect.cs","lineNumber":206,"sourceCode":"\n                var xRelative = x.EndsWith(PercentChar, StringComparison.Ordinal);\n                var yRelative = y.EndsWith(PercentChar, StringComparison.Ordinal);\n                var widthRelative = width.EndsWith(PercentChar, StringComparison.Ordinal);\n                var heightRelative = height.EndsWith(PercentChar, StringComparison.Ordinal);\n\n                if (xRelative && yRelative && widthRelative && heightRelative)\n                {\n                    x = x.TrimEnd(PercentChar);\n                    y = y.TrimEnd(PercentChar);\n                    width = width.TrimEnd(PercentChar);\n                    height = height.TrimEnd(PercentChar);\n\n                    unit = RelativeUnit.Relative;\n                    scale = 0.01;\n                }\n                else if (xRelative || yRelative || widthRelative || heightRelative)\n                {\n                    throw new FormatException(\"If one coordinate is relative, all must be.\");\n                }\n\n                return new RelativeRect(\n                    SpanHelpers.ParseDouble(x, CultureInfo.InvariantCulture) * scale,\n                    SpanHelpers.ParseDouble(y, CultureInfo.InvariantCulture) * scale,\n                    SpanHelpers.ParseDouble(width, CultureInfo.InvariantCulture) * scale,\n                    SpanHelpers.ParseDouble(height, CultureInfo.InvariantCulture) * scale,\n                    unit);\n            }\n        }\n    }\n}\n","sourceCodeStart":188,"sourceCodeEnd":219,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/RelativeRect.cs#L188-L219","documentation":"Thrown by RelativeRect.Parse when some but not all four components (x, y, width, height) carry a '%' suffix. A RelativeRect must be uniformly absolute or uniformly relative; a partially-relative rect like '10,10,50%,20' is rejected as a FormatException.","triggerScenarios":"Parsing a string like \"10, 10, 50%, 50\" via RelativeRect.Parse (from XAML or converter) where one or more — but not all — tokens end in '%'.","commonSituations":"XAML rect markup authored with mixed units intentionally but unsupported by the format; a converter appends '%' to width/height but not x/y; copy-paste between RelativePoint and RelativeRect leaving inconsistent units.","solutions":["Make all four components the same unit: \"10,10,50,50\" or \"10%,10%,50%,50%\".","For partially-relative rects use an explicit layout primitive (RelativeScalar columns/rows or a computed Rect) instead of a single mixed string.","Pre-validate the string splits into 4 tokens with identical suffix handling."],"exampleFix":"<!-- before -->\n<VisualBrush DestinationRect=\"10, 10, 50%, 50\" />\n<!-- after -->\n<VisualBrush DestinationRect=\"10%,10%,50%,50%\" />","handlingStrategy":"validation","validationCode":"static bool IsUniformRect(string s)\n{\n    var parts = s.Split(',', StringSplitOptions.TrimEntries);\n    if (parts.Length != 4) return false;\n    bool rel = parts[0].EndsWith('%');\n    return parts.All(p => p.EndsWith('%') == rel);\n}\nif (!IsUniformRect(s)) throw new FormatException(\"All four rect components must share the same unit (%).\");\nRelativeRect.Parse(s);","typeGuard":null,"tryCatchPattern":"try { RelativeRect.Parse(s); }\ncatch (FormatException) { /* surface markup error */ throw; }","preventionTips":["Keep all four RelativeRect components the same unit in markup.","Validate mixed-unit rect strings before parsing.","Prefer layout primitives for partially-relative rects over a single string."],"tags":["xaml","parsing","relativerect","format-exception","markup"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}