{"record":{"id":"ed1e61f941be23f9","repo":"AvaloniaUI/Avalonia","slug":"child-styles-must-have-a-selector","errorCode":null,"errorMessage":"Child styles must have a selector.","messagePattern":"Child styles must have a selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Style.cs","lineNumber":50,"sourceCode":"        /// </summary>\n        public Selector? Selector \n        {\n            get => _selector;\n            set => _selector = ValidateSelector(value);\n        }\n\n        /// <summary>\n        /// Returns a string representation of the style.\n        /// </summary>\n        /// <returns>A string representation of the style.</returns>\n        public override string ToString() => Selector?.ToString(this) ?? \"Style\";\n\n        internal override void SetParent(StyleBase? parent)\n        {\n            if (parent is Style parentStyle && parentStyle.Selector is not null)\n            {\n                if (Selector is null)\n                    throw new InvalidOperationException(\"Child styles must have a selector.\");\n                Selector.ValidateNestingSelector(false);\n            }\n            else if (parent is ControlTheme)\n            {\n                if (Selector is null)\n                    throw new InvalidOperationException(\"Child styles must have a selector.\");\n                Selector.ValidateNestingSelector(true);\n            }\n\n            base.SetParent(parent);\n        }\n\n        internal SelectorMatchResult TryAttach(StyledElement target, object? host, FrameType type)\n        {\n            _ = target ?? throw new ArgumentNullException(nameof(target));\n\n            var result = SelectorMatchResult.NeverThisType;\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Style.cs#L32-L68","documentation":"Thrown by Style.SetParent when the new parent is a Style that itself has a Selector, but the child Style's Selector is null. A style nested under another (selector-bearing) style must carry its own selector to express what it matches relative to the parent; a selector-less child is invalid in that position.","triggerScenarios":"Adding a `new Style()` (no Selector) as a child of a Style that has a Selector. Parenting happens when the child is added to the parent's Children or when the style tree is assembled.","commonSituations":"Using a child Style as a mere container for setters/resources without intending it to match, under a selector-bearing parent; refactoring that stripped a child's Selector; AXAML nested `<Style>` without a Selector inside a selector-bearing `<Style>`.","solutions":["Give the child style a Selector (typically starting with `^` for nesting), e.g. `new Style(s => s.Nesting().Class(\"foo\"))`.","If the child is only for resources/setters shared unconditionally, put it under a ControlTheme or top-level context instead of under a selector-bearing Style.","In AXAML, ensure every nested `<Style>` has a `Selector` attribute."],"exampleFix":"// before\nvar parent = new Style(s => s.OfType<Button>());\nparent.Children.Add(new Style()); // no Selector\n\n// after\nparent.Children.Add(new Style(s => s.Nesting().Class(\"pointerover\")));","handlingStrategy":"validation","validationCode":"if (parent is Style ps && ps.Selector is not null && childStyle.Selector is null)\n    throw new InvalidOperationException(\"Child style under a selector-bearing Style must have its own Selector.\");","typeGuard":"static bool ChildHasSelector(Style child) => child.Selector is not null;","tryCatchPattern":null,"preventionTips":["Give every nested Style a Selector (usually starting with `^`).","Don't use child Styles as resource-only buckets under selector-bearing parents.","Audit AXAML nested `<Style>` elements for missing Selector attributes."],"tags":["avalonia","styling","style","child-style","selector"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}