{"record":{"id":"3392e265a076f886","repo":"AvaloniaUI/Avalonia","slug":"container-cannot-be-added-as-a-nested-style","errorCode":null,"errorMessage":"Container cannot be added as a nested style.","messagePattern":"Container cannot be added as a nested style\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/ContainerQuery.cs","lineNumber":63,"sourceCode":"        /// </summary>\n        public string? Name\n        {\n            get => _name;\n            set => _name = value;\n        }\n\n        /// <summary>\n        /// Returns a string representation of the container.\n        /// </summary>\n        /// <returns>A string representation of the container.</returns>\n        public override string ToString() => Query?.ToString(this) ?? \"ContainerQuery\";\n\n        internal override void SetParent(StyleBase? parent)\n        {\n            if (parent is ControlTheme)\n                base.SetParent(parent);\n            else\n                throw new InvalidOperationException(\"Container cannot be added as a nested style.\");\n        }\n\n        internal SelectorMatchResult TryAttach(StyledElement target, object? host, FrameType type)\n        {\n            _ = target ?? throw new ArgumentNullException(nameof(target));\n\n            var result = SelectorMatchResult.NeverThisType;\n\n            if (HasChildren)\n            {\n                var match = Query?.Match(target, Parent, true, Name) ??\n                    (target == host ?\n                        SelectorMatch.AlwaysThisInstance :\n                        SelectorMatch.NeverThisInstance);\n\n                if (match.IsMatch)\n                {\n                    Attach(target, match.Activator, type, true);","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/ContainerQuery.cs#L45-L81","documentation":"Error \"Container cannot be added as a nested style.\" thrown in AvaloniaUI/Avalonia.","triggerScenarios":"Raised when a ContainerQuery is nested inside another style. Defend by declaring container queries only at the top level of a Styles collection and validating style trees before applying them.","commonSituations":"See trigger scenarios.","solutions":["Move the ContainerQuery to a top-level Styles collection instead of nesting it inside another style.","Use a regular Style with a container query selector if conditional styling is required."],"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"}