{"record":{"id":"5ef94081c5c1ec8d","repo":"AvaloniaUI/Avalonia","slug":"controltheme-style-may-not-directly-contain-a-chil","errorCode":null,"errorMessage":"ControlTheme style may not directly contain a child or descendent selector.","messagePattern":"ControlTheme style may not directly contain a child or descendent selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Selector.cs","lineNumber":118,"sourceCode":"\n        /// <summary>\n        /// Moves to the previous selector.\n        /// </summary>\n        private protected abstract Selector? MovePrevious();\n\n        /// <summary>\n        /// Moves to the previous selector or the parent selector.\n        /// </summary>\n        private protected abstract Selector? MovePreviousOrParent();\n\n        internal virtual void ValidateNestingSelector(bool inControlTheme, int templateCount = 0)\n        {\n            var s = this;\n\n            if (inControlTheme)\n            {\n                if (!s.InTemplate && s.IsCombinator)\n                    throw new InvalidOperationException(\n                        \"ControlTheme style may not directly contain a child or descendent selector.\");\n                if (s is TemplateSelector && templateCount++ > 0)\n                    throw new InvalidOperationException(\n                        \"ControlTemplate styles cannot contain multiple template selectors.\");\n            }\n\n            var previous = s.MovePreviousOrParent();\n\n            if (previous is null)\n            {\n                if (s is not NestingSelector)\n                    throw new InvalidOperationException(\"Child styles must have a nesting selector.\");\n            }\n            else\n            {\n                previous.ValidateNestingSelector(inControlTheme, templateCount);\n            }\n        }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Selector.cs#L100-L136","documentation":"Thrown during ValidateNestingSelector when a selector chain belonging to a style nested in a ControlTheme contains a child (`>`) or descendant combinator outside of a `:template()` scope. ControlThemes target a single control instance, so relating to children/descendants of that control is only meaningful after entering the control's template via the Template selector.","triggerScenarios":"Defining a ControlTheme child style with a selector like `Button > ContentPresenter` or `^ TextBlock` (descendant) without first traversing `:template()`. The check fires when `!InTemplate && IsCombinator` while inControlTheme is true.","commonSituations":"Writing theme styles that try to reach into the visual tree the way application-level Styles do; forgetting to add `/template/ ` before addressing template parts; converting an app Style to a ControlTheme without adjusting combinators.","solutions":["Add a Template selector step before the combinator, e.g. `Button /template/ ContentPresenter > TextBlock`.","Move child/descendant rules that target arbitrary visual-tree nodes out of the ControlTheme into an application-level Style.","Re-read the selector chain and ensure every combinator is preceded by `:template()` when inside a ControlTheme."],"exampleFix":"// before (inside ControlTheme, invalid)\nnew Style(s => s.OfType<Button>().Child().OfType<ContentPresenter>())\n\n// after (enter template first)\nnew Style(s => s.OfType<Button>().Template().OfType<ContentPresenter>())","handlingStrategy":"validation","validationCode":"// Ensure every combinator in a ControlTheme selector is preceded by a Template step.\nstatic bool SelectorValidForTheme(Selector s)\n{\n    // Walk the chain; combinators are only allowed after InTemplate becomes true.\n    return true; // implement per-chain walk using InTemplate/IsCombinator","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Inside ControlThemes, always address template parts via `:template()` before any `>` or descendant combinator.","Review theme selectors for stray child/descendant combinators.","Keep application-level child/descendant rules in app Styles, not ControlThemes."],"tags":["avalonia","styling","controltheme","combinator","template-selector"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}