AvaloniaUI/Avalonia · error · InvalidOperationException
Cannot set direct property '{Property}' in '{instance.Source
Error message
Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator. What it means
Thrown by Setter.Instance when the target property is a DirectProperty and the style instance has an activator (i.e. the style is conditional, gated by a selector that is not AlwaysThisInstance). Direct properties are set unconditionally via a direct setter delegate and do not participate in the priority/value-store activator system, so they cannot be toggled on/off by a style's activator. The message names the property and the style source for diagnosis.
Source
Thrown at src/Avalonia.Base/Styling/Setter.cs:76
_value = value;
}
}
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;View on GitHub (pinned to 11c5427268)
Solutions
- Move the direct-property Setter into a ControlTheme or a style without an activator (selector that always matches this instance).
- Convert the direct property to a StyledProperty if it must be conditionally set by a style.
- Use a different mechanism (e.g. code-behind reacting to pseudo-classes) for conditional direct-property changes.
Example fix
// before (conditional style setting a direct property)
styles.Add(new Style(s => s.OfType<MyControl>().Class("foo"))
{
Setters = { new Setter(MyControl.MyDirectProperty, 42) }
});
// after (move into the control's ControlTheme, which has no activator)
theme.Setters.Add(new Setter(MyControl.MyDirectProperty, 42)); Defensive patterns
Strategy: validation
Validate before calling
foreach (var setter in style.Setters.OfType<Setter>())
if (setter.Property?.IsDirect == true && styleHasActivator)
throw new InvalidOperationException($"Direct property {setter.Property} cannot be set in a conditional style."); Type guard
static bool IsDirectProperty(AvaloniaProperty? p) => p?.IsDirect == true;
Prevention
- Keep direct-property Setters in ControlThemes or activator-less styles.
- Convert properties that must be conditionally styled from DirectProperty to StyledProperty.
- Document which properties are direct vs styled in custom controls.
When it happens
Trigger: Putting a Setter for a direct property (e.g. a control's direct AvaloniaProperty) inside a Style that has a selector producing an activator (most non-trivial selectors like `.Class(...)`, `:pointerover`, etc.). Direct properties can only be set in an activator-less style (e.g. inside a ControlTheme applied unconditionally).
Common situations: Migrating a direct property assignment from a ControlTheme into a conditional Style; targeting a custom DirectProperty from a class-level style; assuming direct and styled properties behave the same under conditional styles.
Related errors
- Cannot set Class Binding property '(Classes.{classPropertyNa
- Don't know how to instance a style on this type.
- Setter.Property must be set.
- Setter value '{Value}' is not a valid value for property '{P
- Property '{property.Name} not registered on '{o.GetType()}
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/df7b35e5dc47873b.
Report an issue: GitHub.