{"record":{"id":"f3708f2062655012","repo":"dotnet/wpf","slug":"sr-size-widthcannotbenegative","errorCode":null,"errorMessage":"SR.Size_WidthCannotBeNegative","messagePattern":"SR\\.Size_WidthCannotBeNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs","lineNumber":250,"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 Width\n        {\n            get\n            {\n                return _width;\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_WidthCannotBeNegative);\n                }\n\n                _width = value;\n            }\n        }\n\n        /// <summary>\n        /// Height - The Height component of the Size.  This cannot be set to negative, and will only\n        /// 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","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs#L232-L268","documentation":"Rect.Width's setter validates that the assigned value is non-negative and throws ArgumentException otherwise. Width is a magnitude in WPF geometry; negative extents are not representable (flipping is done by swapping points, not negative sizes). This check runs only after the IsEmpty guard, so it fires for real rects given negative widths.","triggerScenarios":"Assigning rect.Width = value where value < 0 — e.g. computing width from right-left with swapped coordinates, or binding Width to a value that can go negative during a transition.","commonSituations":"Rubber-band selection code that doesn't normalize drag direction; resizing logic subtracting in the wrong order; data-binding feeding unvalidated numeric input into a Rect-backed property.","solutions":["Clamp or normalize: rect.Width = Math.Max(0, computedWidth).","Normalize direction: use Math.Abs(right - left), or build via new Rect(Point1, Point2) which orders points itself.","Validate the source value before assignment and fix the computation that produced a negative."],"exampleFix":"// before\nrect.Width = rightEdge - rect.X; // ArgumentException when dragged left\n// after\nrect.X = Math.Min(rect.X, rightEdge);\nrect.Width = Math.Abs(rightEdge - Math.Min(rect.X, rightEdge));","handlingStrategy":"validation","validationCode":"if (newWidth >= 0 && !rect.IsEmpty) { rect.Width = newWidth; }","typeGuard":"static bool IsValidWidth(double w) => !double.IsNaN(w) && w >= 0;","tryCatchPattern":"try { rect.Width = w; }\ncatch (ArgumentException) { rect.Width = Math.Max(0, w); }","preventionTips":["Clamp computed widths with Math.Max(0, value).","Derive width from Math.Abs(right - left) to survive direction changes.","Watch for data-binding or animation paths that can deliver negative values."],"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"}