{"record":{"id":"02124b4236417651","repo":"dotnet/wpf","slug":"sr-format-sr-uielement-layout-positiveinfinityreturned-this","errorCode":null,"errorMessage":"SR.Format(SR.UIElement_Layout_PositiveInfinityReturned, this.GetType().FullName)","messagePattern":"SR\\.Format\\(SR\\.UIElement_Layout_PositiveInfinityReturned, this\\.GetType\\(\\)\\.FullName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs","lineNumber":672,"sourceCode":"\n                        _previousAvailableSize = availableSize;\n\n                        layoutManager.ExitMeasure();\n\n                        if (gotException)\n                        {\n                            // we don't want to reset last exception element on layoutManager if it's been already set.\n                            if (layoutManager.GetLastExceptionElement() == null)\n                            {\n                                layoutManager.SetLastExceptionElement(this);\n                            }\n                        }\n                    }\n\n                    //enforce that MeasureCore can not return PositiveInfinity size even if given Infinte availabel size.\n                    //Note: NegativeInfinity can not be returned by definition of Size structure.\n                    if (double.IsPositiveInfinity(desiredSize.Width) || double.IsPositiveInfinity(desiredSize.Height))\n                        throw new InvalidOperationException(SR.Format(SR.UIElement_Layout_PositiveInfinityReturned, this.GetType().FullName));\n\n                    //enforce that MeasureCore can not return NaN size .\n                    if (double.IsNaN(desiredSize.Width) || double.IsNaN(desiredSize.Height))\n                        throw new InvalidOperationException(SR.Format(SR.UIElement_Layout_NaNReturned, this.GetType().FullName));\n\n                    //reset measure dirtiness\n\n                    MeasureDirty = false;\n                    //reset measure request.\n                    if (MeasureRequest != null)\n                        ContextLayoutManager.From(Dispatcher).MeasureQueue.Remove(this);\n\n                    //cache desired size\n                    _desiredSize = desiredSize;\n\n                    //notify parent if our desired size changed (watefall effect)\n                    if (!MeasureDuringArrange\n                       && !DoubleUtil.AreClose(prevSize, desiredSize))","sourceCodeStart":654,"sourceCodeEnd":690,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L654-L690","documentation":"After MeasureCore returns, UIElement.Measure validates the desiredSize and throws InvalidOperationException if width or height is PositiveInfinity, even when infinite available size was given. A MeasureCore implementation must always return a finite desired size; infinity is not a legal measure result.","triggerScenarios":"A custom FrameworkElement/panel whose MeasureOverride returns Size(double.PositiveInfinity, ...) or propagates the infinite constraint back as desired size.","commonSituations":"Custom controls returning the availableSize directly instead of a finite measured size; content-based elements measuring 'infinite' children and forwarding infinity.","solutions":["Fix MeasureOverride to return a finite desired size (e.g. clamp or measure content with the constraint).","Never return availableSize itself when it is infinite; measure children and sum finite results.","Use double.IsInfinity in custom code to sanitize the returned Size."],"exampleFix":"// before\nprotected override Size MeasureOverride(Size available) => available; // may be infinite\n// after\nprotected override Size MeasureOverride(Size available)\n{\n    child.Measure(available);\n    return new Size(Math.Min(child.DesiredSize.Width, available.Width), child.DesiredSize.Height);\n}","handlingStrategy":"validation","validationCode":"var d = MeasureOverrideCore(available);\nif (double.IsPositiveInfinity(d.Width) || double.IsPositiveInfinity(d.Height))\n    d = new Size(Math.Min(d.Width, available.Width), Math.Min(d.Height, available.Height));","typeGuard":"bool IsFiniteDesiredSize(Size s) => !double.IsInfinity(s.Width) && !double.IsInfinity(s.Height) && !double.IsNaN(s.Width) && !double.IsNaN(s.Height);","tryCatchPattern":"try { element.Measure(available); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"PositiveInfinity\")) { /* fix MeasureOverride of the named type */ }","preventionTips":["Never return the constraint Size directly from MeasureOverride","Always return finite desired sizes","Clamp results to available size where applicable"],"tags":["wpf","layout","measure","custom-controls"],"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"}