{"record":{"id":"9e213e71dcb09640","repo":"tui-cs/Terminal.Gui","slug":"subviews-of-margin-are-not-supported","errorCode":null,"errorMessage":"SubViews of Margin are not supported.","messagePattern":"SubViews of Margin are not supported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/ViewBase/View.Hierarchy.cs","lineNumber":221,"sourceCode":"        ArgumentOutOfRangeException.ThrowIfGreaterThan (index, InternalSubViews.Count);\n\n        //Debug.Assert (view.SuperView is null, $\"{view} already has a SuperView: {view.SuperView}.\");\n        if (view.SuperView is { })\n        {\n            Logging.Warning ($\"{view} already has a SuperView: {view.SuperView}.\");\n        }\n\n        //Debug.Assert (!InternalSubViews.Contains (view), $\"{view} has already been Added to {this}.\");\n        if (InternalSubViews.Contains (view))\n        {\n            Logging.Warning ($\"{view} has already been Added to {this}.\");\n        }\n\n        if (this is MarginView)\n        {\n            if (view is not ShadowView)\n            {\n                throw new InvalidOperationException (\"SubViews of Margin are not supported.\");\n            }\n        }\n\n        if (!RaiseSubViewAdding (view))\n        {\n            return null;\n        }\n\n        // TODO: Make this thread safe\n        InternalSubViews.Insert (index, view);\n\n        // Try to set the SuperView - this may be cancelled\n        if (!view.SetSuperView (this))\n        {\n            InternalSubViews.RemoveAt (index);\n\n            // The change was cancelled\n            return null;","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/ViewBase/View.Hierarchy.cs#L203-L239","documentation":"MarginView is the outermost adornment of a View and only accepts ShadowView as a SubView. Adding any other view type throws because the margin is a strictly controlled structural element, not a general-purpose container.","triggerScenarios":"Calling margin.Add(someView), view.Margin.Add(someView), or AddAt on a MarginView instance with anything other than a ShadowView.","commonSituations":"Mistaking the Margin adornment for a regular container; trying to draw decorations inside the margin programmatically; generic traversal code that recurses into all SubViews and tries to re-add them.","solutions":["Add the view to the main View body or to a FrameView/Window, not to Margin.","If you need a shadow effect, add a ShadowView to the Margin explicitly.","Check 'view is MarginView' before calling Add in generic/adornment-traversal code."],"exampleFix":"// before\nsomeView.Margin.Add(myLabel);\n// after\nsomeView.Add(myLabel);","handlingStrategy":"type-guard","validationCode":"if (target is not MarginView) container.Add (child);","typeGuard":"static bool CanAcceptSubView (View host, View child) => host is not MarginView || child is ShadowView;","tryCatchPattern":null,"preventionTips":["Never call Add/AddAt on a MarginView directly.","Add content views to the View body.","Check 'host is MarginView' in generic traversal code."],"tags":["hierarchy","adornment","margin","subviews"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}