dotnet/wpf · error · ArgumentException
Width and Height cannot be negative.
Error message
Width and Height cannot be negative.
What it means
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.
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
Example fix
// before var box = new SizeBox(availableWidth - margin, availableHeight - margin); // can be negative // after var w = Math.Max(0, availableWidth - margin); var h = Math.Max(0, availableHeight - margin); var box = new SizeBox(w, h);
Defensive patterns
Strategy: validation
Validate before calling
if (double.IsNaN(width) || double.IsNaN(height) || width < 0 || height < 0)
{
width = Math.Max(0, width);
height = Math.Max(0, height);
} Type guard
static bool IsValidBoxedSize(double width, double height)
=> !double.IsNaN(width) && !double.IsNaN(height) && width >= 0 && height >= 0; Try / catch
try
{
var box = new SizeBox(width, height);
}
catch (ArgumentException)
{
// negative dimension from layout math; clamp and retry
var box = new SizeBox(Math.Max(0, width), Math.Max(0, height));
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- SR.Rect_WidthAndHeightCannotBeNegative
- SR.Format(SR.InvalidCtorParameterNoNegative, "value")
- 0x80040206
- args
- args
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/780bf7b843deaf0c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/KnownBoxes.cs:14
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Windows;
namespace MS.Internal.KnownBoxes
{
internal class SizeBox
{
internal SizeBox(double width, double height)
{
if (width < 0 || height < 0)
{
throw new System.ArgumentException(SR.Rect_WidthAndHeightCannotBeNegative);
}
_width = width;
_height = height;
}
internal SizeBox(Size size): this(size.Width, size.Height) {}
internal double Width
{
get
{
return _width;
}
set
{
if (value < 0)
{View on GitHub (pinned to 81131a70a4)