{"record":{"id":"80202626c3844180","repo":"dotnet/wpf","slug":"sr-rect-widthandheightcannotbenegative","errorCode":null,"errorMessage":"SR.Rect_WidthAndHeightCannotBeNegative","messagePattern":"SR\\.Rect_WidthAndHeightCannotBeNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/KnownBoxes.cs","lineNumber":33,"sourceCode":"            }\n\n            _width = width;\n            _height = height;\n        }\n\n        internal SizeBox(Size size): this(size.Width, size.Height) {}\n\n        internal double Width  \n        { \n            get \n            { \n                return _width; \n            }\n            set\n            {\n                if (value < 0)\n                {\n                    throw new System.ArgumentException(SR.Rect_WidthAndHeightCannotBeNegative);\n                }\n\n                _width = value;\n            }\n        }\n\n        internal double Height \n        { \n            get \n            { \n                return _height; \n            }\n            set\n            {\n                if (value < 0)\n                {\n                    throw new System.ArgumentException(SR.Rect_WidthAndHeightCannotBeNegative);\n                }","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/KnownBoxes.cs#L15-L51","documentation":"The Width setter of WPF's internal SizeBox enforces the non-negative dimension invariant: assigning a negative value throws ArgumentException (SR.Rect_WidthAndHeightCannotBeNegative). This keeps cached boxed sizes valid, since a negative width is not a legal Size dimension in WPF.","triggerScenarios":"Setting SizeBox.Width to any value < 0, usually a width derived from layout arithmetic (available minus padding/margins) without clamping.","commonSituations":"Negative results from subtracting chrome/decorations from available space; uninitialized or sentinel values (-1) being assigned as a real width; data-driven widths computed from unvalidated input.","solutions":["Clamp before assignment: box.Width = Math.Max(0, computedWidth)","Validate the computed width and substitute an empty/default size when negative","Fix the upstream calculation that produces the negative width"],"exampleFix":"// before\nbox.Width = availableWidth - totalPadding; // may be negative -> ArgumentException\n\n// after\nbox.Width = Math.Max(0, availableWidth - totalPadding);","handlingStrategy":"validation","validationCode":"if (width >= 0)\n{\n    box.Width = width;\n}\nelse\n{\n    box.Width = 0; // or skip the assignment\n}","typeGuard":"static bool IsValidWidth(double width)\n    => !double.IsNaN(width) && width >= 0;","tryCatchPattern":"try\n{\n    box.Width = computedWidth;\n}\ncatch (ArgumentException)\n{\n    box.Width = 0;\n}","preventionTips":["Assign Math.Max(0, value) instead of raw computed widths","Filter out sentinel values like -1 before assignment","Centralize size computations in one helper that guarantees non-negative output"],"tags":["wpf","layout","size","argument-out-of-range"],"backgroundTag":"argument-out-of-range","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"}