AvaloniaUI/Avalonia · error · InvalidOperationException

Cannot add {child.GetType()} to a style.

Error message

Cannot add {child.GetType()} to a style.

What it means

Thrown by StyleBase's IAddChild.AddChild when an object added as content of a Style/ControlTheme is neither a SetterBase (Setter/Animation setter) nor an IStyle (nested style). The XAML content of a Style is restricted to setters and child styles; anything else is rejected. The message includes the offending object's runtime type.

Source

Thrown at src/Avalonia.Base/Styling/StyleBase.cs:86

        internal bool HasChildren => _children?.Count > 0;
        internal bool HasSettersOrAnimations => _setters?.Count > 0 || _animations?.Count > 0;

        public void Add(SetterBase setter) => Setters.Add(setter);
        public void Add(IStyle style) => Children.Add(style);

        void IAddChild.AddChild(object child)
        {
            switch (child)
            {
                case SetterBase setter:
                    Setters.Add(setter);
                    break;
                case IStyle style:
                    Children.Add(style);
                    break;
                default:
                    throw new InvalidOperationException($"Cannot add {child.GetType()} to a style.");
            }
        }

        public event EventHandler? OwnerChanged;

        public bool TryGetResource(object key, ThemeVariant? themeVariant, out object? result)
        {
            if (_resources is not null && _resources.TryGetResource(key, themeVariant, out result))
                return true;

            if (_children is not null)
            {
                for (var i = 0; i < _children.Count; ++i)
                {
                    if (_children[i].TryGetResource(key, themeVariant, out result))
                        return true;
                }
            }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Keep Style/ControlTheme content to `<Setter>` (and setter-like) elements and nested `<Style>` elements only.
  2. Move stray visual elements out of the Style to the correct parent control/template.
  3. If adding resources, use `<Style.Resources>` rather than top-level content.

Example fix

<!-- before -->
<Style Selector="Button">
  <TextBlock Text="hi"/>
</Style>

<!-- after -->
<Style Selector="Button">
  <Setter Property="Content" Value="hi"/>
</Style>
Defensive patterns

Strategy: type-guard

Validate before calling

static void AddChildSafe(StyleBase style, object child)
{
    switch (child)
    {
        case SetterBase: case IStyle: break;
        default: throw new InvalidOperationException($"{child.GetType()} cannot be added to a style.");
    }
    ((IAddChild)style).AddChild(child);
}

Type guard

static bool IsStyleContent(object child) => child is SetterBase or IStyle;

Prevention

When it happens

Trigger: Putting an unsupported object as the content of a `<Style>` or `<ControlTheme>` in AXAML (e.g. a raw `<TextBlock>`, a `<Grid>`, a resource, or a CLR object). Also via manual IAddChild.AddChild calls with a wrong type.

Common situations: AXAML where a control or non-setter element is mistakenly nested under a Style; copy-paste moving visual elements into a style; custom markup producing unexpected object types.

Related errors


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