{"record":{"id":"43de29dc32d573f0","repo":"AvaloniaUI/Avalonia","slug":"cannot-register-a-non-attached-property-as-attache","errorCode":null,"errorMessage":"Cannot register a non-attached property as attached.","messagePattern":"Cannot register a non-attached property as attached\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/AvaloniaPropertyRegistry.cs","lineNumber":490,"sourceCode":"\n        /// <summary>\n        /// Registers an attached <see cref=\"AvaloniaProperty\"/> on a type.\n        /// </summary>\n        /// <param name=\"type\">The type.</param>\n        /// <param name=\"property\">The property.</param>\n        /// <remarks>\n        /// You won't usually want to call this method directly, instead use the\n        /// <see cref=\"AvaloniaProperty.RegisterAttached{THost, TValue}(string, Type, TValue, bool, Data.BindingMode, Func{TValue, bool}, Func{AvaloniaObject, TValue, TValue})\"/>\n        /// method.\n        /// </remarks>\n        public void RegisterAttached(Type type, AvaloniaProperty property)\n        {\n            _ = type ?? throw new ArgumentNullException(nameof(type));\n            _ = property ?? throw new ArgumentNullException(nameof(property));\n            \n            if (!property.IsAttached)\n            {\n                throw new InvalidOperationException(\n                    \"Cannot register a non-attached property as attached.\");\n            }\n            lock (_unregisteringLocker)\n            {\n                if (!_attached.TryGetValue(type, out var inner))\n                {\n                    inner = new Dictionary<int, AvaloniaProperty>();\n                    inner.Add(property.Id, property);\n                    _attached.Add(type, inner);\n                }\n                else\n                {\n                    inner.Add(property.Id, property);\n                }\n            \n                _attachedCache.Clear();\n                _inheritedCache.Clear();\n            }","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/AvaloniaPropertyRegistry.cs#L472-L508","documentation":"Thrown by AvaloniaPropertyRegistry.RegisterAttached when the property being registered has IsAttached == false. Attached and non-attached properties are stored in separate dictionaries; registering a normal styled/direct property through the attached path is a programming error.","triggerScenarios":"Calling RegisterAttached(type, property) with a property created via AvaloniaProperty.Register (not RegisterAttached); passing a regular StyledProperty or DirectProperty into the attached-registration API.","commonSituations":"Custom control code that wires registration manually and picks the wrong Register overload; copy-paste of a RegisterAttached call site applied to a non-attached property definition.","solutions":["Register non-attached properties with Register(type, property) instead of RegisterAttached.","If the property should be attached, create it with AvaloniaProperty.RegisterAttached so IsAttached is true.","Guard with a check on property.IsAttached before choosing the registration API."],"exampleFix":"// before\nregistry.RegisterAttached(ownerType, MyControl.FooProperty); // FooProperty is non-attached\n\n// after\nif (property.IsAttached) registry.RegisterAttached(ownerType, property);\nelse registry.Register(ownerType, property);","handlingStrategy":"validation","validationCode":"if (property.IsAttached) registry.RegisterAttached(type, property); else registry.Register(type, property);","typeGuard":"static bool IsAttachedProperty(AvaloniaProperty p) => p.IsAttached;","tryCatchPattern":null,"preventionTips":["Branch registration on property.IsAttached.","Create attached properties with RegisterAttached, regular ones with Register.","Avoid manually wiring RegisterAttached for non-attached properties."],"tags":["avalonia","property-system","registration","attached-property"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}