{"record":{"id":"df7b35e5dc47873b","repo":"AvaloniaUI/Avalonia","slug":"cannot-set-direct-property-property-in-insta","errorCode":null,"errorMessage":"Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator.","messagePattern":"Cannot set direct property '(.+?)' in '(.+?)' because the style has an activator\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Setter.cs","lineNumber":76,"sourceCode":"                _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\n                return this;\n        }\n\n        bool IValueEntry.HasValue() => true;","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Setter.cs#L58-L94","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before (conditional style setting a direct property)\nstyles.Add(new Style(s => s.OfType<MyControl>().Class(\"foo\"))\n{\n    Setters = { new Setter(MyControl.MyDirectProperty, 42) }\n});\n\n// after (move into the control's ControlTheme, which has no activator)\ntheme.Setters.Add(new Setter(MyControl.MyDirectProperty, 42));","handlingStrategy":"validation","validationCode":"foreach (var setter in style.Setters.OfType<Setter>())\n    if (setter.Property?.IsDirect == true && styleHasActivator)\n        throw new InvalidOperationException($\"Direct property {setter.Property} cannot be set in a conditional style.\");","typeGuard":"static bool IsDirectProperty(AvaloniaProperty? p) => p?.IsDirect == true;","tryCatchPattern":null,"preventionTips":["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."],"tags":["avalonia","styling","setter","direct-property","activator"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}