{"record":{"id":"83955d6140e4d5e5","repo":"dotnet/wpf","slug":"sr-format-sr-namescopenotfound-name-register-83955d","errorCode":null,"errorMessage":"SR.Format(SR.NameScopeNotFound, name, \"register\")","messagePattern":"SR\\.Format\\(SR\\.NameScopeNotFound, name, \"register\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs","lineNumber":71,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Registers the name - element combination from the\n        /// NameScope that the current element belongs to.\n        /// </summary>\n        /// <param name=\"name\">Name of the element</param>\n        /// <param name=\"scopedElement\">Element where name is defined</param>\n        public void RegisterName(string name, object scopedElement)\n        {\n            INameScope nameScope = FrameworkElement.FindScope(this);\n            if (nameScope != null)\n            {\n                nameScope.RegisterName(name, scopedElement);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, \"register\"));\n            }\n        }\n\n        /// <summary>\n        /// Unregisters the name - element combination from the\n        /// NameScope that the current element belongs to.\n        /// </summary>\n        /// <param name=\"name\">Name of the element</param>\n        public void UnregisterName(string name)\n        {\n            INameScope nameScope = FrameworkElement.FindScope(this);\n            if (nameScope != null)\n            {\n                nameScope.UnregisterName(name);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, \"unregister\"));","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs#L53-L89","documentation":"FrameworkElement.RegisterName requires the element to be inside a name scope (usually established by an XAML page/template). FindNameScope() returned null, so there is no INameScope in which to register the name, and WPF throws InvalidOperationException naming the element and the 'register' operation.","triggerScenarios":"Calling element.RegisterName(name, scopedElement) on a FrameworkElement whose FindNameScope() is null — i.e. the element is not yet attached to a name-scoped tree (e.g. not added to a Window/Page that defines a NameScope).","commonSituations":"Registering names on a freshly constructed element created in code before adding it to the visual/logical tree; calling RegisterName during construction or before the element is loaded; elements outside any XAML namescope such as free-floating controls.","solutions":["Add the element to its parent tree (Window, Page, Control template content) before calling RegisterName so a NameScope exists.","Register names on the root element that owns the namescope (e.g. window.RegisterName(...)) instead of a detached child.","Alternatively use NameScope.SetNameScope(element, new NameScope()) to attach a scope manually for code-only trees."],"exampleFix":"// before\nvar tb = new TextBlock { Name = \"tb1\" };\ntb.RegisterName(\"tb1\", tb); // throws: no namescope yet\nroot.Children.Add(tb);\n\n// after\nvar tb = new TextBlock { Name = \"tb1\" };\nroot.Children.Add(tb);          // attach first -> namescope available\nroot.RegisterName(\"tb1\", tb);","handlingStrategy":"validation","validationCode":"if (element.FindNameScope() == null)\n    throw new InvalidOperationException(\"Element is not in a NameScope; attach it to the tree first.\");\nelement.RegisterName(name, scopedElement);","typeGuard":"bool CanRegisterName(FrameworkElement e) => e.FindNameScope() != null;","tryCatchPattern":"try\n{\n    element.RegisterName(name, scopedElement);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"register\"))\n{\n    Log.Warn($\"No NameScope for '{name}'; element not attached to tree.\", ex);\n}","preventionTips":["Register names only after the element is added to a Window/Page/control tree.","Use the scope-owning root (Window/Page) for RegisterName calls in code-only trees.","For detached trees, attach a scope explicitly with NameScope.SetNameScope."],"tags":["wpf","namescope","xaml","invalid-operation"],"backgroundTag":"resource-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}