{"record":{"id":"97b73b0f8138a9a2","repo":"AvaloniaUI/Avalonia","slug":"expected-control-name-to-be-typeof-t-but-i","errorCode":null,"errorMessage":"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.","messagePattern":"Expected control '(.+?)' to be '(.+?) but it was '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/NameScopeExtensions.cs","lineNumber":38,"sourceCode":"        public static T? Find<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                return null;\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        /// Finds a named element in an <see cref=\"INameScope\"/>.\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 or null if not found.</returns>\n        public static T? Find<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            var styledAnchor = anchor as StyledElement;\n            if (styledAnchor == null)\n                return null;","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/NameScopeExtensions.cs#L20-L56","documentation":"Thrown by NameScopeExtensions.Find<T>(INameScope, string) when a control with the requested name exists in the scope but is not assignable to the requested generic type T. Find<T> returns null when nothing is found, but treats a type mismatch as a programming error.","triggerScenarios":"Calling nameScope.Find<TextBox>(\"myButton\") where \"myButton\" is registered as a Button. The name resolves but result is T fails.","commonSituations":"XAML x:Name was changed to a different control type but the C# Find<T> call was not updated. Refactoring a view swapped a control type (e.g., TextBlock to Label) while Find<T> still requests the old type. Generic type inference picks the wrong T.","solutions":["Align the generic type parameter with the actual registered control type in XAML.","If you are unsure of the type, use the non-generic Find(name) and check/cast manually.","If the control type genuinely varies, use Find<object> or Find<IControl> and narrow at runtime."],"exampleFix":"// before\nvar box = nameScope.Find<TextBox>(\"myLabel\"); // throws, it's a Label\n// after\nvar label = nameScope.Find<Label>(\"myLabel\");","handlingStrategy":"type-guard","validationCode":"var found = nameScope.Find(name);\nif (found is T typed) return typed;\nreturn null; // or handle the type mismatch explicitly","typeGuard":"static T? SafeFind<T>(INameScope scope, string name) where T : class\n{\n    var found = scope.Find(name);\n    return found as T; // null if wrong type, no throw\n}","tryCatchPattern":"try { return nameScope.Find<T>(name); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Expected control\"))\n{ return null; }","preventionTips":["Keep the generic Find<T> type parameter in sync with the XAML control type.","Prefer Find(name) + 'is T' pattern when the type is uncertain.","When refactoring a XAML control type, grep for matching Find<T> calls."],"tags":["namescope","type-mismatch","xaml","controls"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}