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/ControlTheme.cs:48

        /// Gets or sets a control theme that is the basis of the current theme.
        /// </summary>
        public ControlTheme? BasedOn { get; set; }

        public override string ToString() => TargetType?.Name ?? "ControlTheme";

        internal override void SetParent(StyleBase? parent)
        {
            throw new InvalidOperationException("ControlThemes cannot be added as a nested style.");
        }

        internal SelectorMatchResult TryAttach(StyledElement target, FrameType type)
        {
            Debug.Assert(type is FrameType.Theme or FrameType.TemplatedParentTheme);

            _ = target ?? throw new ArgumentNullException(nameof(target));

            if (TargetType is null)
                throw new InvalidOperationException("ControlTheme has no TargetType.");

            using var activity = Diagnostic.AttachingStyle()?
                .AddTag(Diagnostic.Tags.Style, this);
            
            if (HasSettersOrAnimations && TargetType.IsAssignableFrom(target.StyleKey))
            {
                Attach(target, null, type, true);
                activity?.AddTag(Diagnostic.Tags.SelectorResult, SelectorMatchResult.AlwaysThisType);

                return SelectorMatchResult.AlwaysThisType;
            }

            activity?.AddTag(Diagnostic.Tags.SelectorResult, SelectorMatchResult.NeverThisType);
            return SelectorMatchResult.NeverThisType;
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Set the ControlTheme's TargetType property to the control type the theme applies to.
  2. Verify the x:Key/TargetType is declared in XAML: <ControlTheme TargetType="Button">.

When it happens

Trigger: Raised when a ControlTheme is evaluated without its TargetType being set. Defend by always setting TargetType on a ControlTheme before it is used and validating themes at load time.

Common situations: See trigger scenarios.


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