{"record":{"id":"be388aad605f7ab8","repo":"AvaloniaUI/Avalonia","slug":"could-not-find-control-name","errorCode":null,"errorMessage":"Could not find control '{name}'.","messagePattern":"Could not find control '(.+?)'\\.","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/NameScopeExtensions.cs","lineNumber":79,"sourceCode":"        /// <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=\"nameScope\">The name scope.</param>\n        /// <param name=\"name\">The name.</param>\n        /// <returns>The named element.</returns>\n        public static T Get<T>(this INameScope nameScope, string name)\n            where T : class\n        {\n            _ = nameScope ?? throw new ArgumentNullException(nameof(nameScope));\n            _ = name ?? throw new ArgumentNullException(nameof(name));\n\n            var result = nameScope.Find(name);\n\n            if (result == null)\n            {\n                throw new KeyNotFoundException($\"Could not find control '{name}'.\");\n            }\n\n            if (result is T typed)\n            {\n                return typed;\n            }\n\n            throw new InvalidOperationException(\n                $\"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.\");\n        }\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>","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/NameScopeExtensions.cs#L61-L97","documentation":"Thrown as a KeyNotFoundException by NameScopeExtensions.Get<T>(INameScope, string) when no element with the given name is registered in the scope. Get<T> (unlike Find<T>) treats a missing name as an error and throws rather than returning null.","triggerScenarios":"Calling nameScope.Get<TextBox>(\"nonExistent\") where no element named \"nonExistent\" is registered. Also fires if Get is called before the template/name scope is populated.","commonSituations":"Typo in the name string vs the x:Name in XAML. Calling Get before the template has been applied (NameScope not yet populated). Name scope is on a different element than expected (wrong anchor). Refactoring removed a named control but left a Get call.","solutions":["Use Find<T> instead of Get<T> if the element may legitimately be absent; handle the null return.","Verify the x:Name in XAML exactly matches the string passed to Get, including casing.","Ensure the template is applied and the correct NameScope is being queried before calling Get."],"exampleFix":"// before\nvar btn = nameScope.Get<Button>(\"saveBtn\"); // KeyNotFound if misspelled\n// after\nvar btn = nameScope.Find<Button>(\"saveBtn\");\nif (btn is null) return; // or handle gracefully","handlingStrategy":"validation","validationCode":"var found = nameScope.Find(name);\nif (found is null)\n    throw new InvalidOperationException($\"'{name}' not registered.\");\n// proceed, or use Find and handle null gracefully","typeGuard":null,"tryCatchPattern":"try { return nameScope.Get<T>(name); }\ncatch (KeyNotFoundException ex) when (ex.Message.Contains(\"Could not find\"))\n{ return null; // or default }","preventionTips":["Use Find<T> when absence is expected; reserve Get<T> for names guaranteed to exist.","Verify the exact x:Name spelling and casing before calling Get.","Ensure the template is applied and the correct NameScope is queried."],"tags":["namescope","not-found","key-not-found","xaml"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}