dotnet/wpf · error · InvalidOperationException
SR.Rect_CannotModifyEmptyRect
Error message
SR.Rect_CannotModifyEmptyRect
What it means
Rect.Location's setter throws InvalidOperationException when the rect is the special Empty rect. The empty rect is a singleton (all fields NaN) with no defined position, so modifying its location is meaningless and would corrupt the sentinel representation. WPF exposes this as an invalid-state error rather than silently assigning.
Solutions
- Assign a whole new Rect instead of mutating: rect = new Rect(point, size).
- Check rect.IsEmpty before setting Location and handle the empty case explicitly.
- Initialize fields with a real rect (e.g. Rect(0, 0, w, h)) instead of Rect.Empty/default.
Example fix
// before
if (hitRect.IsEmpty) { hitRect.Location = origin; } // throws
// after
if (!hitRect.IsEmpty) { hitRect.Location = origin; } else { hitRect = new Rect(origin, hitSize); } Defensive patterns
Strategy: validation
Validate before calling
if (!rect.IsEmpty) { rect.Location = new Point(x, y); } Type guard
static bool IsMutable(Rect r) => !r.IsEmpty;
Try / catch
try { rect.Location = p; }
catch (InvalidOperationException) { rect = new Rect(p, rect.Size == default ? new Size(0,0) : rect.Size); } Prevention
- Check IsEmpty before any per-field mutation of a Rect.
- Avoid default(Rect) and Rect.Empty as mutable placeholders; initialize with real geometry.
- Prefer constructing a new Rect over mutating Location/X/Y on shared values.
When it happens
Trigger: Assigning rect.Location = new Point(...) while rect == Rect.Empty, typically after a default Rect field or an Intersection/Union that produced Empty.
Common situations: Storing a Rect in a class field initialized to default(Rect) or Rect.Empty and later setting Location directly; hit-test results returning Empty when nothing was hit; layout code that forgot to initialize a rect.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.Rect_CannotCallMethod
- Current DocumentSequence, FixedDocument, or FixedPage not…
- IAmbientProvider
- InvalidOperationException()
- IXamlSchemaContextProvider
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f1867c926989462a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs:148
return _width < 0;
}
}
/// <summary>
/// Location - The Point representing the origin of the Rectangle
/// </summary>
public Point Location
{
get
{
return new Point(_x, _y);
}
set
{
if (IsEmpty)
{
throw new System.InvalidOperationException(SR.Rect_CannotModifyEmptyRect);
}
_x = value._x;
_y = value._y;
}
}
/// <summary>
/// Size - The Size representing the area of the Rectangle
/// </summary>
public Size Size
{
get
{
if (IsEmpty)
return Size.Empty;
return new Size(_width, _height);
}View on GitHub (pinned to 81131a70a4)