dotnet/wpf · error · ArgumentException
SR.Size_WidthCannotBeNegative
Error message
SR.Size_WidthCannotBeNegative
What it means
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.
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.
Example fix
// before rect.Width = rightEdge - rect.X; // ArgumentException when dragged left // after rect.X = Math.Min(rect.X, rightEdge); rect.Width = Math.Abs(rightEdge - Math.Min(rect.X, rightEdge));
Defensive patterns
Strategy: validation
Validate before calling
if (newWidth >= 0 && !rect.IsEmpty) { rect.Width = newWidth; } Type guard
static bool IsValidWidth(double w) => !double.IsNaN(w) && w >= 0;
Try / catch
try { rect.Width = w; }
catch (ArgumentException) { rect.Width = Math.Max(0, w); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SR.Size_HeightCannotBeNegative
- SR.Size_WidthAndHeightCannotBeNegative
- ' ' is not a valid value for ' '.
- ArgumentException(message, name)
- ArgumentNullException(name)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f3708f2062655012.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs:250
/// be negative if this is the empty rectangle, in which case it will be negative infinity.
/// If this rect is Empty, setting this property is illegal.
/// </summary>
public double Width
{
get
{
return _width;
}
set
{
if (IsEmpty)
{
throw new System.InvalidOperationException(SR.Rect_CannotModifyEmptyRect);
}
if (value < 0)
{
throw new System.ArgumentException(SR.Size_WidthCannotBeNegative);
}
_width = value;
}
}
/// <summary>
/// Height - The Height component of the Size. This cannot be set to negative, and will only
/// be negative if this is the empty rectangle, in which case it will be negative infinity.
/// If this rect is Empty, setting this property is illegal.
/// </summary>
public double Height
{
get
{
return _height;
}
setView on GitHub (pinned to 81131a70a4)