{"record":{"id":"a65f3bab8342859d","repo":"AvaloniaUI/Avalonia","slug":"controltemplate-styles-cannot-contain-multiple-tem","errorCode":null,"errorMessage":"ControlTemplate styles cannot contain multiple template selectors.","messagePattern":"ControlTemplate styles cannot contain multiple template selectors\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Selector.cs","lineNumber":121,"sourceCode":"        /// </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        }\n\n        private static SelectorMatch MatchUntilCombinator(\n            StyledElement control,","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Selector.cs#L103-L139","documentation":"Thrown during ValidateNestingSelector when a selector chain inside a ControlTheme contains more than one TemplateSelector. A ControlTheme can only enter its control's template once; chaining multiple `:template()` steps is structurally invalid because there is no nested template to enter.","triggerScenarios":"Writing a selector like `Button /template/ ContentPresenter /template/ TextBlock` inside a ControlTheme. The counter increments on the first TemplateSelector and the second triggers the throw.","commonSituations":"Assuming template parts have their own templates that can be re-entered; copy-pasting a template selector segment; building selectors programmatically and concatenating two Template() calls.","solutions":["Use exactly one Template selector per ControlTheme selector chain; address deeper parts with child/descendant combinators after the single `:template()`.","If you need to style a nested templated control, define a separate ControlTheme for that control type instead of chaining template selectors."],"exampleFix":"// before (two template selectors)\nnew Style(s => s.OfType<Button>().Template().OfType<ContentPresenter>().Template().OfType<TextBlock>())\n\n// after (single template, then descendant)\nnew Style(s => s.OfType<Button>().Template().Descendant().OfType<TextBlock>())","handlingStrategy":"validation","validationCode":"// Count Template selectors in the chain; reject if > 1 before assigning.\nint CountTemplates(Selector? s)\n{\n    int n = 0;\n    while (s is not null) { if (s is TemplateSelector) n++; s = s.MoveNextPrevious(); }\n    return n;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use at most one `:template()` per ControlTheme selector chain.","For nested templated controls, author a separate ControlTheme.","Lint selectors for duplicate Template tokens."],"tags":["avalonia","styling","controltheme","template-selector","validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}