{"record":{"id":"f93b04d8c2892908","repo":"AvaloniaUI/Avalonia","slug":"cannot-add-child-gettype-to-a-style","errorCode":null,"errorMessage":"Cannot add {child.GetType()} to a style.","messagePattern":"Cannot add (.+?) to a style\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/StyleBase.cs","lineNumber":86,"sourceCode":"\n        internal bool HasChildren => _children?.Count > 0;\n        internal bool HasSettersOrAnimations => _setters?.Count > 0 || _animations?.Count > 0;\n\n        public void Add(SetterBase setter) => Setters.Add(setter);\n        public void Add(IStyle style) => Children.Add(style);\n\n        void IAddChild.AddChild(object child)\n        {\n            switch (child)\n            {\n                case SetterBase setter:\n                    Setters.Add(setter);\n                    break;\n                case IStyle style:\n                    Children.Add(style);\n                    break;\n                default:\n                    throw new InvalidOperationException($\"Cannot add {child.GetType()} to a style.\");\n            }\n        }\n\n        public event EventHandler? OwnerChanged;\n\n        public bool TryGetResource(object key, ThemeVariant? themeVariant, out object? result)\n        {\n            if (_resources is not null && _resources.TryGetResource(key, themeVariant, out result))\n                return true;\n\n            if (_children is not null)\n            {\n                for (var i = 0; i < _children.Count; ++i)\n                {\n                    if (_children[i].TryGetResource(key, themeVariant, out result))\n                        return true;\n                }\n            }","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/StyleBase.cs#L68-L104","documentation":"Thrown by StyleBase's IAddChild.AddChild when an object added as content of a Style/ControlTheme is neither a SetterBase (Setter/Animation setter) nor an IStyle (nested style). The XAML content of a Style is restricted to setters and child styles; anything else is rejected. The message includes the offending object's runtime type.","triggerScenarios":"Putting an unsupported object as the content of a `<Style>` or `<ControlTheme>` in AXAML (e.g. a raw `<TextBlock>`, a `<Grid>`, a resource, or a CLR object). Also via manual IAddChild.AddChild calls with a wrong type.","commonSituations":"AXAML where a control or non-setter element is mistakenly nested under a Style; copy-paste moving visual elements into a style; custom markup producing unexpected object types.","solutions":["Keep Style/ControlTheme content to `<Setter>` (and setter-like) elements and nested `<Style>` elements only.","Move stray visual elements out of the Style to the correct parent control/template.","If adding resources, use `<Style.Resources>` rather than top-level content."],"exampleFix":"<!-- before -->\n<Style Selector=\"Button\">\n  <TextBlock Text=\"hi\"/>\n</Style>\n\n<!-- after -->\n<Style Selector=\"Button\">\n  <Setter Property=\"Content\" Value=\"hi\"/>\n</Style>","handlingStrategy":"type-guard","validationCode":"static void AddChildSafe(StyleBase style, object child)\n{\n    switch (child)\n    {\n        case SetterBase: case IStyle: break;\n        default: throw new InvalidOperationException($\"{child.GetType()} cannot be added to a style.\");\n    }\n    ((IAddChild)style).AddChild(child);\n}","typeGuard":"static bool IsStyleContent(object child) => child is SetterBase or IStyle;","tryCatchPattern":null,"preventionTips":["Restrict Style/ControlTheme content to Setter elements and nested Styles.","Move visual elements to the proper parent control or template.","Use Style.Resources for resources instead of top-level content."],"tags":["avalonia","styling","iaddchild","xaml","type-mismatch"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}