{"record":{"id":"40d957839cf739ad","repo":"dotnet/wpf","slug":"sr-uielement-layout-nanmeasure","errorCode":null,"errorMessage":"SR.UIElement_Layout_NaNMeasure","messagePattern":"SR\\.UIElement_Layout_NaNMeasure","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs","lineNumber":579,"sourceCode":"            {\n                perfElementID = PerfService.GetPerfElementID(this);\n\n                etwTracingEnabled = true;\n                EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientMeasureElementBegin, EventTrace.Keyword.KeywordLayout, EventTrace.Level.Verbose, perfElementID, availableSize.Width, availableSize.Height);\n            }\n            try\n            {\n                //             VerifyAccess();\n\n                // Disable reentrancy during the measure pass.  This is because much work is done\n                // during measure - such as inflating templates, formatting PTS stuff, creating\n                // fonts, etc.  Generally speaking, we cannot survive reentrancy in these code\n                // paths.\n                using (Dispatcher.DisableProcessing())\n                {\n                    //enforce that Measure can not receive NaN size .\n                    if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))\n                        throw new InvalidOperationException(SR.UIElement_Layout_NaNMeasure);\n\n                    bool neverMeasured = NeverMeasured;\n\n                    if (neverMeasured)\n                    {\n                        switchVisibilityIfNeeded(this.Visibility);\n                        //to make sure effects are set correctly - otherwise it's not used\n                        //simply because it is never pulled by anybody\n                        pushVisualEffects();\n                    }\n\n                    bool isCloseToPreviousMeasure = DoubleUtil.AreClose(availableSize, _previousAvailableSize);\n\n\n                    //if Collapsed, we should not Measure, keep dirty bit but remove request\n                    if (this.Visibility == Visibility.Collapsed\n                        || ((Visual)this).CheckFlagsAnd(VisualFlags.IsLayoutSuspended))\n                    {","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L561-L597","documentation":"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.","triggerScenarios":"Calling Measure(new Size(double.NaN, h)) directly; a parent panel passing NaN from its own bad math; custom panels forwarding unconstrained NaN dimensions.","commonSituations":"Custom layout code that computes available size with 0.0/0 divisions or uninitialized doubles; calling Measure outside normal UpdateLayout flow.","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."],"exampleFix":"// before\nelement.Measure(new Size(double.NaN, double.NaN));\n// after\nelement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));","handlingStrategy":"validation","validationCode":"if (double.IsNaN(available.Width) || double.IsNaN(available.Height))\n    available = new Size(double.IsNaN(available.Width) ? double.PositiveInfinity : available.Width,\n                         double.IsNaN(available.Height) ? double.PositiveInfinity : available.Height);","typeGuard":"bool IsFiniteSize(Size s) => !double.IsNaN(s.Width) && !double.IsNaN(s.Height);","tryCatchPattern":"try { element.Measure(size); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"NaN\")) { /* sanitize size and retry */ }","preventionTips":["Never pass NaN to Measure; use PositiveInfinity for unconstrained","Guard custom panel math against 0/0","Let UpdateLayout drive measurement when possible"],"tags":["wpf","layout","measure"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}