{"record":{"id":"5866fc883e5127d6","repo":"dotnet/wpf","slug":"sr-templatefindnameininvalidelement","errorCode":null,"errorMessage":"SR.TemplateFindNameInInvalidElement","messagePattern":"SR\\.TemplateFindNameInInvalidElement","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs","lineNumber":304,"sourceCode":"\n        /// <summary>\n        /// FindName - Finds the element associated with the id defined under this control template.\n        ///          Context of the FrameworkElement where this template is applied will be passed\n        ///          as parameter.\n        /// </summary>\n        /// <param name=\"name\">string name</param>\n        /// <param name=\"templatedParent\">context where this template is applied</param>\n        /// <returns>the element associated with the Name</returns>\n        public Object FindName(string name, FrameworkElement templatedParent)\n        {\n            // Verify Context Access\n            VerifyAccess();\n\n            ArgumentNullException.ThrowIfNull(templatedParent);\n\n            if (this != templatedParent.TemplateInternal)\n            {\n                throw new InvalidOperationException(SR.TemplateFindNameInInvalidElement);\n            }\n\n            return StyleHelper.FindNameInTemplateContent(templatedParent, name, this);\n        }\n\n        #endregion PublicProperties\n\n\n        #region INameScope\n\n        /// <summary>\n        /// Registers the name - Context combination\n        /// </summary>\n        /// <param name=\"name\">Name to register</param>\n        /// <param name=\"scopedElement\">Element where name is defined</param>\n        public void RegisterName(string name, object scopedElement)\n        {\n            // Verify Context Access","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L286-L322","documentation":"Template.FindName throws InvalidOperationException (SR.TemplateFindNameInInvalidElement) when the templatedParent argument is not an element instantiated BY this specific template (i.e. templatedParent.TemplateInternal != this). Names inside a template's namescope are only resolvable through the template that created that scope.","triggerScenarios":"Calling template.FindName(name, element) where element's TemplateInternal is a different template instance — e.g. the element uses another template, the template was re-applied (so the instance is stale), or the caller passes the wrong element (a plain content child rather than a templated child).","commonSituations":"Passing a Window/UserControl instead of the templated control; retrieving a template from Application resources but searching within a control that uses a copied/retemplated instance; control template re-applied after theme change leaving a stale template reference.","solutions":["Pass the control that is actually templated by this template instance: use control.Template (or FrameworkElement.FindName directly on it).","Prefer element.FindName(name) or NameScope.GetNameScope on the templated child instead of raw template.FindName.","Ensure the template reference used for FindName is the same instance as control.Template, not a resource lookup that yields a different copy."],"exampleFix":"// before\nvar tpl = (ControlTemplate)Application.Current.Resources[\"tpl\"];\nvar tb = (TextBlock)tpl.FindName(\"text\", myButton); // throws: myButton.TemplateInternal != tpl\n// after\nvar tb = (TextBlock)myButton.Template.FindName(\"text\", myButton);","handlingStrategy":"validation","validationCode":"if (ReferenceEquals(templatedParent?.TemplateInternal, template))\n    var found = template.FindName(name, templatedParent);","typeGuard":"static bool OwnsTemplate(FrameworkTemplate t, FrameworkElement e) => ReferenceEquals(e?.TemplateInternal, t);","tryCatchPattern":"try { return template.FindName(name, element); }\ncatch (InvalidOperationException) { return element?.FindName(name); }","preventionTips":["Use control.Template (the live instance) for FindName, not a separate resource lookup.","Prefer element.FindName for simple lookups inside templates.","Re-fetch the template after a theme change that re-applies templates."],"tags":["wpf","templates","namescopes","invalid-argument-value"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}