{"record":{"id":"29083c0b629c25b3","repo":"AvaloniaUI/Avalonia","slug":"cannot-set-name-styled-element-already-styled","errorCode":null,"errorMessage":"Cannot set Name : styled element already styled.","messagePattern":"Cannot set Name : styled element already styled\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/StyledElement.cs","lineNumber":171,"sourceCode":"        /// <inheritdoc />\n        public event EventHandler? ActualThemeVariantChanged;\n        \n        /// <summary>\n        /// Gets or sets the name of the styled element.\n        /// </summary>\n        /// <remarks>\n        /// An element's name is used to uniquely identify an element within the element's name\n        /// scope. Once the element is added to a logical tree, its name cannot be changed.\n        /// </remarks>\n        public string? Name\n        {\n            get => _name;\n\n            set\n            {\n                if (_stylesApplied)\n                {\n                    throw new InvalidOperationException(\"Cannot set Name : styled element already styled.\");\n                }\n\n                _name = value;\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets the styled element's classes.\n        /// </summary>\n        /// <remarks>\n        /// <para>\n        /// Classes can be used to apply user-defined styling to styled elements, or to allow styled elements\n        /// that share a common purpose to be easily selected.\n        /// </para>\n        /// </remarks>\n        public Classes Classes => _classes ??= new();\n\n        /// <summary>","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/StyledElement.cs#L153-L189","documentation":"Error \"Cannot set Name : styled element already styled.\" thrown in AvaloniaUI/Avalonia.","triggerScenarios":"Raised when the Name property of a styled element is assigned after styling has begun. Defend by setting Name before the element is initialized/added to the tree, and guarding setters against post-init assignment.","commonSituations":"See trigger scenarios.","solutions":["Set the Name property before the element is styled (before it is added to the visual tree and templates are applied).","If the name must change at runtime, unregister and re-register it in the appropriate NameScope instead of setting Name directly."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}