{"record":{"id":"84f3d4683392e9b8","repo":"dotnet/wpf","slug":"sr-size-heightcannotbenegative","errorCode":null,"errorMessage":"SR.Size_HeightCannotBeNegative","messagePattern":"SR\\.Size_HeightCannotBeNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs","lineNumber":277,"sourceCode":"        /// be negative if this is the empty rectangle, in which case it will be negative infinity.\n        /// If this rect is Empty, setting this property is illegal.\n        /// </summary>\n        public double Height\n        {\n            get\n            {\n                return _height;\n            }\n            set\n            {\n                if (IsEmpty)\n                {\n                    throw new System.InvalidOperationException(SR.Rect_CannotModifyEmptyRect);\n                }\n\n                if (value < 0)\n                {\n                    throw new System.ArgumentException(SR.Size_HeightCannotBeNegative);\n                }\n\n                _height = value;\n            }\n        }\n\n        /// <summary>\n        /// Left Property - This is a read-only alias for X\n        /// If this is the empty rectangle, the value will be positive infinity.\n        /// </summary>\n        public double Left\n        {\n            get\n            {\n                return _x;\n            }\n        }\n","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs#L259-L295","documentation":"Rect.Height's setter validates the assigned value is non-negative and throws ArgumentException otherwise. Heights are magnitudes in WPF geometry; negative values indicate a computation bug (e.g. bottom computed above top) and are rejected to keep layout math well-defined.","triggerScenarios":"Assigning rect.Height = value where value < 0 — commonly from bottom - top with swapped coordinates, or a scaled/animated value that transiently dips below zero.","commonSituations":"Drag-resize handling that doesn't account for dragging above/upward; zoom or scale transforms producing negative extents; parsing external data with negative extents.","solutions":["Clamp: rect.Height = Math.Max(0, computedHeight).","Normalize with Math.Abs(bottom - top) or use the Rect(Point, Point) constructor that swaps points as needed.","Fix the upstream coordinate computation so bottom/top are ordered before deriving height."],"exampleFix":"// before\nrect.Height = bottom - rect.Y; // ArgumentException when dragged up\n// after\nvar top = Math.Min(rect.Y, bottom);\nrect.Y = top;\nrect.Height = Math.Abs(bottom - top);","handlingStrategy":"validation","validationCode":"if (newHeight >= 0 && !rect.IsEmpty) { rect.Height = newHeight; }","typeGuard":"static bool IsValidHeight(double h) => !double.IsNaN(h) && h >= 0;","tryCatchPattern":"try { rect.Height = h; }\ncatch (ArgumentException) { rect.Height = Math.Max(0, h); }","preventionTips":["Compute height as Math.Abs(bottom - top) and set Y to Math.Min(top, bottom).","Clamp scaled/animated heights to >= 0 before assignment.","Validate external/serialized extents before constructing rects from them."],"tags":["wpf","rect","argument-validation","negative-value"],"backgroundTag":"value-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"}