{"record":{"id":"4e4b03f739d1a021","repo":"lepoco/wpfui","slug":"iconelement-should-have-only-1-child","errorCode":null,"errorMessage":"IconElement should have only 1 child","messagePattern":"IconElement should have only 1 child","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/IconElement/IconElement.cs","lineNumber":73,"sourceCode":"    private void EnsureLayoutRoot()\n    {\n        if (_layoutRoot != null)\n        {\n            return;\n        }\n\n        _layoutRoot = new Grid { Background = Brushes.Transparent, SnapsToDevicePixels = true };\n\n        _ = _layoutRoot.Children.Add(InitializeChildren());\n\n        AddVisualChild(_layoutRoot);\n    }\n\n    protected override Visual GetVisualChild(int index)\n    {\n        if (index != 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(index), \"IconElement should have only 1 child\");\n        }\n\n        EnsureLayoutRoot();\n        return _layoutRoot!;\n    }\n\n    protected override Size MeasureOverride(Size availableSize)\n    {\n        EnsureLayoutRoot();\n\n        _layoutRoot!.Measure(availableSize);\n        return _layoutRoot.DesiredSize;\n    }\n\n    protected override Size ArrangeOverride(Size finalSize)\n    {\n        EnsureLayoutRoot();\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/IconElement/IconElement.cs#L55-L91","documentation":"Thrown by IconElement.GetVisualChild when WPF (or a derived class) requests a visual child at an index other than 0. IconElement is contractually single-child: it hosts exactly one Grid (_layoutRoot). Requesting index 1+ violates the VisualChildrenCount=1 contract and indicates either a subclass that added visuals without overriding the count, or a framework caller bug.","triggerScenarios":"A subclass of IconElement overrides VisualChildrenCount or adds visual children without overriding GetVisualChild; WPF measure/arrange or an ancestor panel calls GetVisualChild with an out-of-range index because the reported count was inflated.","commonSituations":"Custom IconElement subclass that adds extra children; an adorner or decorator mishandling the child count of an IconElement descendant; very rarely a WPF layout bug interacting with custom icon controls.","solutions":["If subclassing IconElement, keep VisualChildrenCount at 1 or override GetVisualChild to return the correct child for each index.","Do not call AddVisualChild on an IconElement subclass without also overriding the count and GetVisualChild.","If you are a consumer (not a subclasser), report the issue — this path should be unreachable through normal API use.","Verify no adorner layer is injecting children into an icon element."],"exampleFix":"// before (broken subclass)\npublic class MyIcon : IconElement {\n    private readonly Visual _extra;\n    protected override int VisualChildrenCount => 2; // GetVisualChild(1) throws\n}\n\n// after\nprotected override int VisualChildrenCount => 1;\nprotected override Visual GetVisualChild(int index) =>\n    index == 0 ? _layoutRoot! : throw new ArgumentOutOfRangeException(nameof(index));","handlingStrategy":"validation","validationCode":"// Framework invariant — not user-callable. If subclassing IconElement:\nprotected override int VisualChildrenCount => 1;\nprotected override Visual GetVisualChild(int index) =>\n    index == 0 ? base.GetVisualChild(0)\n               : throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When subclassing IconElement, keep VisualChildrenCount consistent with the children you add.","Never call AddVisualChild on an IconElement subclass without overriding GetVisualChild and the count.","Avoid adorners that inject children into icon elements."],"tags":["wpf","iconelement","visual-tree","subclass-contract"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}