dotnet/wpf · error · InvalidOperationException
SR.UIElement_Layout_NaNMeasure
Error message
SR.UIElement_Layout_NaNMeasure
What it means
UIElement.Measure throws an InvalidOperationException when the availableSize passed to it contains NaN for width or height. WPF layout forbids NaN constraints because downstream math (desired size, arrange) cannot produce defined results. Measure is a framework-enforced entry point, so the check happens before MeasureCore runs.
Solutions
- Replace NaN in available size with double.PositiveInfinity (unconstrained) or a finite value before calling Measure.
- Fix the parent panel's computation that produced NaN (usually 0/0 or NaN propagation).
- Prefer letting UpdateLayout drive Measure instead of calling it manually.
Example fix
// before element.Measure(new Size(double.NaN, double.NaN)); // after element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Defensive patterns
Strategy: validation
Validate before calling
if (double.IsNaN(available.Width) || double.IsNaN(available.Height))
available = new Size(double.IsNaN(available.Width) ? double.PositiveInfinity : available.Width,
double.IsNaN(available.Height) ? double.PositiveInfinity : available.Height); Type guard
bool IsFiniteSize(Size s) => !double.IsNaN(s.Width) && !double.IsNaN(s.Height);
Try / catch
try { element.Measure(size); } catch (InvalidOperationException ex) when (ex.Message.Contains("NaN")) { /* sanitize size and retry */ } Prevention
- Never pass NaN to Measure; use PositiveInfinity for unconstrained
- Guard custom panel math against 0/0
- Let UpdateLayout drive measurement when possible
When it happens
Trigger: Calling Measure(new Size(double.NaN, h)) directly; a parent panel passing NaN from its own bad math; custom panels forwarding unconstrained NaN dimensions.
Common situations: Custom layout code that computes available size with 0.0/0 divisions or uninitialized doubles; calling Measure outside normal UpdateLayout flow.
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
- SR.Format(SR.UIElement_Layout_NaNReturned…
- SR.Format(SR.UIElement_Layout_PositiveInfinityReturned…
- 0x80040206
- ArgumentNullException: child
- SR.ArrangeReentrancyInvalid
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/40d957839cf739ad.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs:579
{
perfElementID = PerfService.GetPerfElementID(this);
etwTracingEnabled = true;
EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientMeasureElementBegin, EventTrace.Keyword.KeywordLayout, EventTrace.Level.Verbose, perfElementID, availableSize.Width, availableSize.Height);
}
try
{
// VerifyAccess();
// Disable reentrancy during the measure pass. This is because much work is done
// during measure - such as inflating templates, formatting PTS stuff, creating
// fonts, etc. Generally speaking, we cannot survive reentrancy in these code
// paths.
using (Dispatcher.DisableProcessing())
{
//enforce that Measure can not receive NaN size .
if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))
throw new InvalidOperationException(SR.UIElement_Layout_NaNMeasure);
bool neverMeasured = NeverMeasured;
if (neverMeasured)
{
switchVisibilityIfNeeded(this.Visibility);
//to make sure effects are set correctly - otherwise it's not used
//simply because it is never pulled by anybody
pushVisualEffects();
}
bool isCloseToPreviousMeasure = DoubleUtil.AreClose(availableSize, _previousAvailableSize);
//if Collapsed, we should not Measure, keep dirty bit but remove request
if (this.Visibility == Visibility.Collapsed
|| ((Visual)this).CheckFlagsAnd(VisualFlags.IsLayoutSuspended))
{View on GitHub (pinned to 81131a70a4)