dotnet/wpf · error · ArgumentException
SR.Size_HeightCannotBeNegative
Error message
SR.Size_HeightCannotBeNegative
What it means
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.
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.
Example fix
// before rect.Height = bottom - rect.Y; // ArgumentException when dragged up // after var top = Math.Min(rect.Y, bottom); rect.Y = top; rect.Height = Math.Abs(bottom - top);
Defensive patterns
Strategy: validation
Validate before calling
if (newHeight >= 0 && !rect.IsEmpty) { rect.Height = newHeight; } Type guard
static bool IsValidHeight(double h) => !double.IsNaN(h) && h >= 0;
Try / catch
try { rect.Height = h; }
catch (ArgumentException) { rect.Height = Math.Max(0, h); } Prevention
- 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.
When it happens
Trigger: Assigning rect.Height = value where value < 0 — commonly from bottom - top with swapped coordinates, or a scaled/animated value that transiently dips below zero.
Common situations: Drag-resize handling that doesn't account for dragging above/upward; zoom or scale transforms producing negative extents; parsing external data with negative extents.
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_WidthCannotBeNegative
- 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/84f3d4683392e9b8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs:277
/// 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;
}
set
{
if (IsEmpty)
{
throw new System.InvalidOperationException(SR.Rect_CannotModifyEmptyRect);
}
if (value < 0)
{
throw new System.ArgumentException(SR.Size_HeightCannotBeNegative);
}
_height = value;
}
}
/// <summary>
/// Left Property - This is a read-only alias for X
/// If this is the empty rectangle, the value will be positive infinity.
/// </summary>
public double Left
{
get
{
return _x;
}
}
View on GitHub (pinned to 81131a70a4)