AvaloniaUI/Avalonia · error · InvalidOperationException

Cannot set Class Binding property '(Classes.{classPropertyNa

Error message

Cannot set Class Binding property '(Classes.{classPropertyName})' in '{instance.Source}' because the style has an activator.

What it means

Thrown by Setter.Instance when the target property is a Classes-binding property (the `Classes.XXX` pseudo-property used to bind a class membership to an expression) and the style instance has an activator. Binding a class from within a conditional style would create a circular/ambiguous activation, so Avalonia forbids it. The message includes the class property name and the style source.

Source

Thrown at src/Avalonia.Base/Styling/Setter.cs:79

        AvaloniaProperty IValueEntry.Property => EnsureProperty();

        public override string ToString() => $"Setter: {Property} = {Value}";

        void IValueEntry.Unsubscribe() { }

        [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConversionSupressWarningMessage)]
        internal override ISetterInstance Instance(IStyleInstance instance, StyledElement target)
        {
            if (target is not AvaloniaObject ao)
                throw new InvalidOperationException("Don't know how to instance a style on this type.");
            if (Property is null)
                throw new InvalidOperationException("Setter.Property must be set.");
            if (Property.IsDirect && instance.HasActivator)
                throw new InvalidOperationException(
                    $"Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator.");
            if (Property.IsClassesBindingProperty(out var classPropertyName) && instance.HasActivator)
                throw new InvalidOperationException(
                        $"Cannot set Class Binding property '(Classes.{classPropertyName})' in '{instance.Source}' because the style has an activator.");

            if (Value is BindingBase binding)
                return SetBinding((StyleInstance)instance, ao, binding);
            else if (Value is ITemplate template && !typeof(ITemplate).IsAssignableFrom(Property.PropertyType))
                return new PropertySetterTemplateInstance(Property, template);
            else if (!Property.IsValidValue(Value))
                throw new InvalidCastException($"Setter value '{Value}' is not a valid value for property '{Property}'.");
            else if (Property.IsDirect)
                return SetDirectValue(target);
            else
                return this;
        }

        bool IValueEntry.HasValue() => true;
        object? IValueEntry.GetValue() => Value;

        bool IValueEntry.GetDataValidationState(out BindingValueType state, out Exception? error)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Move the Classes-binding Setter into a ControlTheme or an activator-less style.
  2. Drive the class through code-behind or a different binding strategy instead of a conditional style setter.
  3. Re-evaluate whether the class should be bound at all; prefer setting it directly.

Example fix

// before (conditional style binding a class)
new Style(s => s.OfType<Control>().Class("a"))
{
    Setters = { new Setter(Classes.bProperty, someBinding) }
}

// after (activator-less ControlTheme setter)
theme.Setters.Add(new Setter(Classes.bProperty, someBinding));
Defensive patterns

Strategy: validation

Validate before calling

foreach (var setter in style.Setters.OfType<Setter>())
    if (setter.Property?.IsClassesBindingProperty(out _) == true && styleHasActivator)
        throw new InvalidOperationException("Classes-binding properties cannot be set in a conditional style.");

Type guard

static bool IsClassesBinding(AvaloniaProperty? p) => p?.IsClassesBindingProperty(out _) == true;

Prevention

When it happens

Trigger: A Setter targeting `Classes.SomeClass` (a Classes binding property) inside a Style whose selector yields an activator (conditional match). IsClassesBindingProperty returns true and instance.HasActivator is true.

Common situations: Trying to drive one pseudo-class/class from another class within a conditional style; binding Classes in a Style rather than in a ControlTheme or activator-less context.

Related errors


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