AvaloniaUI/Avalonia · error · InvalidOperationException

ControlTheme has no TargetType.

Error message

ControlTheme has no TargetType.

What it means

Error "ControlTheme has no TargetType." thrown in AvaloniaUI/Avalonia.

Source

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

    /// </summary>
    internal class NestingSelector : Selector
    {
        internal override bool InTemplate => false;
        internal override bool IsCombinator => false;
        internal override Type? TargetType => null;

        public override string ToString(Style? owner) => owner?.Parent?.ToString() ?? "^";

        private protected override SelectorMatch Evaluate(StyledElement control, IStyle? parent, bool subscribe)
        {
            if (parent is Style s && s.Selector is not null)
            {
                return s.Selector.Match(control, s.Parent, subscribe);
            }
            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;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Set the ControlTheme's TargetType property before using a nesting selector inside it.
  2. Replace the nesting selector (^) with an explicit selector if no parent ControlTheme TargetType exists.

When it happens

Trigger: Raised when a nesting selector is resolved against a ControlTheme that has no TargetType. Defend by setting TargetType on the ControlTheme so nested selectors can be built against a concrete type.

Common situations: See trigger scenarios.


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