AvaloniaUI/Avalonia · error · InvalidOperationException

Nesting selector was specified but cannot determine parent s

Error message

Nesting selector was specified but cannot determine parent selector.

What it means

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.

Source

Thrown at src/Avalonia.Base/Styling/NestingSelector.cs:39

            }
            else if (parent is ControlTheme theme)
            {
                if (theme.TargetType is null)
                    throw new InvalidOperationException("ControlTheme has no TargetType.");
                return theme.TargetType.IsAssignableFrom(control.StyleKey) ?
                    SelectorMatch.AlwaysThisType :
                    SelectorMatch.NeverThisType;
            }
            else if (parent is ContainerQuery query && query.Parent is ControlTheme queryTheme)
            {
                if (queryTheme.TargetType is null)
                    throw new InvalidOperationException("ControlTheme has no TargetType.");
                return queryTheme.TargetType.IsAssignableFrom(control.StyleKey) ?
                    SelectorMatch.AlwaysThisType :
                    SelectorMatch.NeverThisType;
            }

            throw new InvalidOperationException(
                "Nesting selector was specified but cannot determine parent selector.");
        }

        private protected override Selector? MovePrevious() => null;
        private protected override Selector? MovePreviousOrParent() => null;
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Move the nested style so its parent is either a Style that has a Selector or a ControlTheme with a TargetType.
  2. If the style is meant to be top-level, replace the `^` nesting selector with an explicit selector (e.g. `Button:pointerover`).
  3. Ensure ContainerQuery nesting ultimately resolves to a ControlTheme parent if relying on theme-based nesting.

Example fix

// before (wrong: ^ at top level)
styles.Add(new Style(s => s.Nesting().Class("foo")) { Setters = { ... } });

// after (explicit selector at top level)
styles.Add(new Style(s => s.OfType<Button>().Class("foo")) { Setters = { ... } });

// or nest it under a parent style
var parent = new Style(s => s.OfType<Button>());
parent.Children.Add(new Style(s => s.Nesting().Class("foo")) { Setters = { ... } });
Defensive patterns

Strategy: validation

Validate before calling

static bool HasValidNestingParent(IStyle? parent) =>
    parent is Style s && s.Selector is not null ||
    parent is ControlTheme ||
    parent is ContainerQuery q && q.Parent is ControlTheme;

Type guard

static bool CanUseNesting(IStyle? parent) =>
    (parent is Style s && s.Selector is not null) || parent is ControlTheme;

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/f4bc0c7eaa3ef9e9. Report an issue: GitHub.