{"record":{"id":"2f301da09aea096b","repo":"AvaloniaUI/Avalonia","slug":"child-styles-must-have-a-nesting-selector","errorCode":null,"errorMessage":"Child styles must have a nesting selector.","messagePattern":"Child styles must have a nesting selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Selector.cs","lineNumber":130,"sourceCode":"        {\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,\n            Selector start,\n            IStyle? parent,\n            bool subscribe,\n            out Selector? combinator)\n        {\n            combinator = null;\n\n            var activators = new AndActivatorBuilder();\n            var result = Match(control, start, parent, subscribe, ref activators, ref combinator);","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Selector.cs#L112-L148","documentation":"Thrown during ValidateNestingSelector when a child style's selector chain, walked back to its leftmost element, does not start with a NestingSelector (`^`). Avalonia requires nested styles to explicitly reference their parent via `^`, so an unanchored child selector is ambiguous and rejected. The leftmost selector is reached via MovePreviousOrParent; if it is null and not a NestingSelector, this fires.","triggerScenarios":"Adding a Style with a selector that omits `^` as a child of another Style or ControlTheme, e.g. a child style whose selector is `.Class(\"foo\")` instead of `^.Class(\"foo\")`.","commonSituations":"Writing nested styles in AXAML and forgetting the `^` prefix; assuming the parent is implicitly prepended; programmatically building child selectors without calling Nesting().","solutions":["Prefix the child selector with the nesting selector: in C# start with `s.Nesting()`, in AXAML start the Selector with `^`.","If the style is not meant to be nested, attach it to the top-level Styles collection instead of as a child."],"exampleFix":"// before (child style missing ^)\nparent.Children.Add(new Style(s => s.Class(\"foo\")) { ... });\n\n// after\nparent.Children.Add(new Style(s => s.Nesting().Class(\"foo\")) { ... });","handlingStrategy":"validation","validationCode":"// Confirm the child selector chain is left-anchored by a NestingSelector before parenting.\nstatic bool StartsAtNesting(Selector? s)\n{\n    Selector? cur = s, prev;\n    do { prev = cur; cur = cur?.MovePreviousOrParent(); } while (cur is not null);\n    return prev is NestingSelector;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always begin nested style selectors with `^` (C#: s.Nesting()).","Visually verify the `^` prefix in AXAML nested styles.","Avoid parenting selector-less styles under selector-bearing parents."],"tags":["avalonia","styling","nesting-selector","child-style","validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}