{"record":{"id":"d688477fd1d8228d","repo":"AvaloniaUI/Avalonia","slug":"don-t-know-how-to-instance-a-style-on-this-type","errorCode":null,"errorMessage":"Don't know how to instance a style on this type.","messagePattern":"Don't know how to instance a style on this type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Setter.cs","lineNumber":72,"sourceCode":"            get => _value;\n            set\n            {\n                (value as ISetterValue)?.Initialize(this);\n                _value = value;\n            }\n        }\n\n        AvaloniaProperty IValueEntry.Property => EnsureProperty();\n\n        public override string ToString() => $\"Setter: {Property} = {Value}\";\n\n        void IValueEntry.Unsubscribe() { }\n\n        [UnconditionalSuppressMessage(\"Trimming\", \"IL2026\", Justification = TrimmingMessages.ImplicitTypeConversionSupressWarningMessage)]\n        internal override ISetterInstance Instance(IStyleInstance instance, StyledElement target)\n        {\n            if (target is not AvaloniaObject ao)\n                throw new InvalidOperationException(\"Don't know how to instance a style on this type.\");\n            if (Property is null)\n                throw new InvalidOperationException(\"Setter.Property must be set.\");\n            if (Property.IsDirect && instance.HasActivator)\n                throw new InvalidOperationException(\n                    $\"Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator.\");\n            if (Property.IsClassesBindingProperty(out var classPropertyName) && instance.HasActivator)\n                throw new InvalidOperationException(\n                        $\"Cannot set Class Binding property '(Classes.{classPropertyName})' in '{instance.Source}' because the style has an activator.\");\n\n            if (Value is BindingBase binding)\n                return SetBinding((StyleInstance)instance, ao, binding);\n            else if (Value is ITemplate template && !typeof(ITemplate).IsAssignableFrom(Property.PropertyType))\n                return new PropertySetterTemplateInstance(Property, template);\n            else if (!Property.IsValidValue(Value))\n                throw new InvalidCastException($\"Setter value '{Value}' is not a valid value for property '{Property}'.\");\n            else if (Property.IsDirect)\n                return SetDirectValue(target);\n            else","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Setter.cs#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nsetter.Instance(instance, new PlainObject()); // PlainObject is not AvaloniaObject\n\n// after\nsetter.Instance(instance, new TestStyledElement()); // derives from StyledElement : AvaloniaObject","handlingStrategy":"type-guard","validationCode":"if (target is not AvaloniaObject)\n    throw new InvalidOperationException(\"Styles can only be applied to AvaloniaObject instances.\");","typeGuard":"static bool IsStyleable(object target) => target is AvaloniaObject;","tryCatchPattern":null,"preventionTips":["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."],"tags":["avalonia","styling","setter","avaloniaobject","type-mismatch"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}