{"record":{"id":"2bff9249a5de1b00","repo":"AvaloniaUI/Avalonia","slug":"the-control-doesn-t-have-an-associated-name-scope","errorCode":null,"errorMessage":"The control doesn't have an associated name scope, probably no registrations has been done yet","messagePattern":"The control doesn't have an associated name scope, probably no registrations has been done yet","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/NameScopeExtensions.cs","lineNumber":107,"sourceCode":"        }\n\n        /// <summary>\n        /// Gets a named element from an <see cref=\"INameScope\"/> or throws if no element of the\n        /// requested name was found.\n        /// </summary>\n        /// <typeparam name=\"T\">The element type.</typeparam>\n        /// <param name=\"anchor\">The control to take the name scope from.</param>\n        /// <param name=\"name\">The name.</param>\n        /// <returns>The named element.</returns>\n        public static T Get<T>(this ILogical anchor, string name)\n            where T : class\n        {\n            _ = anchor ?? throw new ArgumentNullException(nameof(anchor));\n            _ = name ?? throw new ArgumentNullException(nameof(name));\n\n            var nameScope = (anchor as INameScope) ?? NameScope.GetNameScope((StyledElement)anchor);\n            if (nameScope == null)\n                throw new InvalidOperationException(\n                    \"The control doesn't have an associated name scope, probably no registrations has been done yet\");\n            \n            return nameScope.Get<T>(name);\n        }\n        \n        public static INameScope? FindNameScope(this ILogical control)\n        {\n            _ = control ?? throw new ArgumentNullException(nameof(control));\n\n            var scope = control.GetSelfAndLogicalAncestors()\n                .OfType<StyledElement>()\n                .Select(x => (x as INameScope) ?? NameScope.GetNameScope(x))\n                .FirstOrDefault(x => x != null);\n            return scope;\n        }\n    }\n}\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/NameScopeExtensions.cs#L89-L125","documentation":"Thrown by NameScopeExtensions.Get<T>(this ILogical anchor, string name) when the anchor control and its logical ancestors have no associated INameScope. The method walks up the logical tree looking for a NameScope and, finding none, treats the lookup as impossible.","triggerScenarios":"Calling control.Get<T>(\"name\") on a control that is not inside any templated/named tree — e.g., a detached control not yet added to the visual tree, or a control whose ancestors never had a NameScope attached.","commonSituations":"Calling Get/Find on a control in a constructor or Loaded handler before it is attached to the visual/logical tree. Using a plain control (no template) as the anchor where no NameScope was ever set. Querying a control from a different visual tree branch than where the names live.","solutions":["Wait until the control is attached to the visual tree (e.g., in OnAttachedToVisualTree or after Loaded) before calling Get.","Use an anchor that is inside the templated tree where the names are registered.","If you have a direct reference to the NameScope, call nameScope.Get<T>(name) instead of the anchor-based overload."],"exampleFix":"// before (in constructor)\nvar btn = this.Get<Button>(\"saveBtn\"); // no namescope yet\n// after (after attach)\nprotected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)\n{\n    var btn = this.Get<Button>(\"saveBtn\");\n}","handlingStrategy":"validation","validationCode":"var scope = (anchor as INameScope) ?? NameScope.GetNameScope(anchor as StyledElement);\nif (scope is null)\n    throw new InvalidOperationException(\"No NameScope; ensure control is attached to the visual tree.\");\nreturn scope.Get<T>(name);","typeGuard":null,"tryCatchPattern":"try { return anchor.Get<T>(name); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"name scope\"))\n{ /* defer to OnAttachedToVisualTree and retry */ return null; }","preventionTips":["Call Get/Find after the control is attached to the visual tree (OnAttachedToVisualTree / Loaded).","Use an anchor that is inside the templated tree.","If you hold a direct NameScope reference, call nameScope.Get<T>(name) instead."],"tags":["namescope","visual-tree","lifecycle","controls"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}