AvaloniaUI/Avalonia · error · InvalidOperationException
Don't know how to instance a style on this type.
Error message
Don't know how to instance a style on this type.
What it means
Thrown by Setter.Instance when the target object is not an AvaloniaObject. Setters set AvaloniaProperty values, which only exist on AvaloniaObject; a target of any other type cannot receive them. This is a defensive guard against misuse of the styling system on non-Avalonia objects.
Source
Thrown at src/Avalonia.Base/Styling/Setter.cs:72
get => _value;
set
{
(value as ISetterValue)?.Initialize(this);
_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);
elseView on GitHub (pinned to 11c5427268)
Solutions
- Ensure the target is an AvaloniaObject (typically a StyledElement/Control) before attaching styles.
- Do not call Setter.Instance manually; let the styled element's attachment pipeline invoke it.
- If testing, pass a real AvaloniaObject/StyledElement subclass.
Example fix
// before setter.Instance(instance, new PlainObject()); // PlainObject is not AvaloniaObject // after setter.Instance(instance, new TestStyledElement()); // derives from StyledElement : AvaloniaObject
Defensive patterns
Strategy: type-guard
Validate before calling
if (target is not AvaloniaObject)
throw new InvalidOperationException("Styles can only be applied to AvaloniaObject instances."); Type guard
static bool IsStyleable(object target) => target is AvaloniaObject;
Prevention
- Only attach styles to AvaloniaObject/StyledElement targets.
- Do not invoke Setter.Instance manually in app code; use the normal attachment pipeline.
- In tests, use real StyledElement subclasses.
When it happens
Trigger: Attaching a style (via StyleBase.Attach or by adding a styled control's style) to a plain object, a struct, or a non-AvaloniaObject element. In practice rare because the styling pipeline normally only passes StyledElement/AvaloniaObject targets.
Common situations: Custom code that manually drives style attachment on an unsupported object; unit tests that construct a Setter and call Instance with a mock/wrong type; integrating Avalonia styling with a non-Avalonia object model.
Related errors
- Setter value '{Value}' is not a valid value for property '{P
- Setter.Property must be set.
- Cannot set direct property '{Property}' in '{instance.Source
- Cannot set Class Binding property '(Classes.{classPropertyNa
- Cannot add {child.GetType()} to a style.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/d688477fd1d8228d.
Report an issue: GitHub.