dotnet/wpf · error · ArgumentException

SR.InvalidValueForTopLeft

Error message

SR.InvalidValueForTopLeft

What it means

Window.ValidateTopLeft rejects values that cannot denote a window coordinate: PositiveInfinity and NegativeInfinity throw ArgumentException(SR.InvalidValueForTopLeft), and finite values outside Int32 range throw ArgumentException(SR.ValueNotBetweenInt32MinMax). Note that plain NaN and (per the sibling validator) PositiveInfinity may be allowed elsewhere, but infinities are invalid for TopLeft coordinates.

Solutions

  1. Before assigning, reject infinities: if (double.IsInfinity(v)) fall back to a sane default (e.g. Double.NaN or last good position).
  2. Clamp finite values into Int32 range before assignment.
  3. Fix the producer of the bad value (division by zero, unvalidated binding source) or coerce in the binding converter.
  4. Sanitize saved layout/config data at load time with a range and finiteness check.

Example fix

// before
window.Top = computedTop; // could be +Infinity

// after
window.Top = double.IsInfinity(computedTop) || double.IsNaN(computedTop)
    ? Double.NaN
    : Math.Max(Int32.MinValue, Math.Min(Int32.MaxValue, computedTop));
Defensive patterns

Strategy: validation

Validate before calling

bool isValidTopLeft(double v) => !double.IsInfinity(v) && (v >= Int32.MinValue && v <= Int32.MaxValue);

Type guard

double? SafeTopLeft(double v) => double.IsInfinity(v) ? (double?)null : Math.Max(Int32.MinValue, Math.Min(Int32.MaxValue, v));

Try / catch

try { window.Top = value; }
catch (ArgumentException) { window.Top = Double.NaN; /* fall back to default placement */ }

Prevention

When it happens

Trigger: Assigning Window.Top or Window.Left a value of double.PositiveInfinity or double.NegativeInfinity, or a finite double beyond Int32 range — usually via direct assignment, binding, or deserialized config.

Common situations: Binding Top/Left to a property computed with division by zero producing infinity; loading saved layout files where sentinel/infinite values leaked in; JSON/XML deserialization mapping nulls or special values to infinity.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0b8476d9c13f513f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:5685

        private static void ValidateLengthForHeightWidth(double l)
        {
            //basically, NaN and PositiveInfinity are ok, and then anything
            //that can be converted to Int32
            if (!Double.IsPositiveInfinity(l) && !double.IsNaN(l) &&
                ((l > Int32.MaxValue) || (l < Int32.MinValue)))
            {
                throw new ArgumentException(SR.Format(SR.ValueNotBetweenInt32MinMax, l));
            }
        }

        private static void ValidateTopLeft(double length)
        {
            // Values not allowed: PositiveInfinity, NegativeInfinity
            // and values that are beyond the range of Int32
            if (Double.IsPositiveInfinity(length) ||
                Double.IsNegativeInfinity(length))
            {
                throw new ArgumentException(SR.Format(SR.InvalidValueForTopLeft, length));
            }

            if ((length > Int32.MaxValue) ||
                (length < Int32.MinValue))
            {
                throw new ArgumentException(SR.Format(SR.ValueNotBetweenInt32MinMax, length));
            }
        }

        private static void _OnHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window w = d as Window;
            Debug.Assert(w != null, "d must be typeof Window");
            if (w._updateHwndSize)
            {
                w.OnHeightChanged((double) e.NewValue);
            }
        }

View on GitHub (pinned to 81131a70a4)