{"record":{"id":"780bf7b843deaf0c","repo":"dotnet/wpf","slug":"width-and-height-cannot-be-negative","errorCode":null,"errorMessage":"Width and Height cannot be negative.","messagePattern":"Width and Height cannot be negative\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/KnownBoxes.cs","lineNumber":14,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nusing System.Windows;\n\nnamespace MS.Internal.KnownBoxes\n{\n    internal class SizeBox\n    {\n        internal SizeBox(double width, double height)\n        {\n            if (width < 0 || height < 0)\n            {\n                throw new System.ArgumentException(SR.Rect_WidthAndHeightCannotBeNegative);\n            }\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                {","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/KnownBoxes.cs#L1-L32","documentation":"SizeBox is an internal WPF helper that boxes a (width, height) pair so common sizes can be cached and reused without allocation. Its constructor enforces the WPF Size/Rect invariant that dimensions are non-negative, throwing ArgumentException (SR.Rect_WidthAndHeightCannotBeNegative) when either width or height is negative.","triggerScenarios":"Constructing a SizeBox with width < 0 or height < 0 — typically a computed negative dimension (e.g., available size minus larger padding/margins) passed into the boxing helper.","commonSituations":"Layout/measure/arrange math producing negative sizes (subtracting margins or decorations that exceed available space); generalizing code that boxes negative placeholder sizes; NaN/negative results from custom panels flowing into cached-size paths.","solutions":["Clamp computed dimensions with Math.Max(0, value) before constructing the size","Validate width/height at the calculation site and treat negative results as 'no size' rather than a size","Fix the upstream arithmetic that yields the negative dimension"],"exampleFix":"// before\nvar box = new SizeBox(availableWidth - margin, availableHeight - margin); // can be negative\n\n// after\nvar w = Math.Max(0, availableWidth - margin);\nvar h = Math.Max(0, availableHeight - margin);\nvar box = new SizeBox(w, h);","handlingStrategy":"validation","validationCode":"if (double.IsNaN(width) || double.IsNaN(height) || width < 0 || height < 0)\n{\n    width  = Math.Max(0, width);\n    height = Math.Max(0, height);\n}","typeGuard":"static bool IsValidBoxedSize(double width, double height)\n    => !double.IsNaN(width) && !double.IsNaN(height) && width >= 0 && height >= 0;","tryCatchPattern":"try\n{\n    var box = new SizeBox(width, height);\n}\ncatch (ArgumentException)\n{\n    // negative dimension from layout math; clamp and retry\n    var box = new SizeBox(Math.Max(0, width), Math.Max(0, height));\n}","preventionTips":["Clamp every computed dimension with Math.Max(0, value) before boxing","Treat negative layout results as 'no space' rather than a size","Check for NaN alongside negatives when dimensions come from division or subtraction"],"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"}