{"record":{"id":"f4bc0c7eaa3ef9e9","repo":"AvaloniaUI/Avalonia","slug":"nesting-selector-was-specified-but-cannot-determin","errorCode":null,"errorMessage":"Nesting selector was specified but cannot determine parent selector.","messagePattern":"Nesting selector was specified but cannot determine parent selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/NestingSelector.cs","lineNumber":39,"sourceCode":"            }\n            else if (parent is ControlTheme theme)\n            {\n                if (theme.TargetType is null)\n                    throw new InvalidOperationException(\"ControlTheme has no TargetType.\");\n                return theme.TargetType.IsAssignableFrom(control.StyleKey) ?\n                    SelectorMatch.AlwaysThisType :\n                    SelectorMatch.NeverThisType;\n            }\n            else if (parent is ContainerQuery query && query.Parent is ControlTheme queryTheme)\n            {\n                if (queryTheme.TargetType is null)\n                    throw new InvalidOperationException(\"ControlTheme has no TargetType.\");\n                return queryTheme.TargetType.IsAssignableFrom(control.StyleKey) ?\n                    SelectorMatch.AlwaysThisType :\n                    SelectorMatch.NeverThisType;\n            }\n\n            throw new InvalidOperationException(\n                \"Nesting selector was specified but cannot determine parent selector.\");\n        }\n\n        private protected override Selector? MovePrevious() => null;\n        private protected override Selector? MovePreviousOrParent() => null;\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":47,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/NestingSelector.cs#L21-L47","documentation":"Thrown by NestingSelector.Evaluate when the parent of the style containing `^` is none of: a Style with a non-null Selector, a ControlTheme, or a ContainerQuery whose Parent is a ControlTheme. The nesting selector has no resolvable parent context, so it cannot compute a match. This means `^` was used in a style that lacks a valid nesting parent.","triggerScenarios":"Using Selectors.Nesting() / `^` in a top-level style (not nested under another Style or ControlTheme), or parenting a nested style under something that is neither a Style-with-selector nor a ControlTheme (e.g. under a raw IStyle wrapper or a ContainerQuery without a ControlTheme parent).","commonSituations":"AXAML where a nested `<Style Selector=\"^:pointerover\">` is placed at the root Styles collection instead of inside a parent Style/ControlTheme; programmatic style trees where the nesting style is added to the wrong parent; refactoring that moved a nested style out of its parent.","solutions":["Move the nested style so its parent is either a Style that has a Selector or a ControlTheme with a TargetType.","If the style is meant to be top-level, replace the `^` nesting selector with an explicit selector (e.g. `Button:pointerover`).","Ensure ContainerQuery nesting ultimately resolves to a ControlTheme parent if relying on theme-based nesting."],"exampleFix":"// before (wrong: ^ at top level)\nstyles.Add(new Style(s => s.Nesting().Class(\"foo\")) { Setters = { ... } });\n\n// after (explicit selector at top level)\nstyles.Add(new Style(s => s.OfType<Button>().Class(\"foo\")) { Setters = { ... } });\n\n// or nest it under a parent style\nvar parent = new Style(s => s.OfType<Button>());\nparent.Children.Add(new Style(s => s.Nesting().Class(\"foo\")) { Setters = { ... } });","handlingStrategy":"validation","validationCode":"static bool HasValidNestingParent(IStyle? parent) =>\n    parent is Style s && s.Selector is not null ||\n    parent is ControlTheme ||\n    parent is ContainerQuery q && q.Parent is ControlTheme;","typeGuard":"static bool CanUseNesting(IStyle? parent) =>\n    (parent is Style s && s.Selector is not null) || parent is ControlTheme;","tryCatchPattern":null,"preventionTips":["Only use the `^` nesting selector in styles that are children of a Style (with selector) or a ControlTheme.","Prefer explicit selectors for top-level styles.","Keep nested styles physically nested under their parent in AXAML to make the relationship obvious."],"tags":["avalonia","styling","nesting-selector","style-hierarchy"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}