{"record":{"id":"0dcf6c2ee6d8bb57","repo":"dotnet/wpf","slug":"sr-format-sr-namescopenotfound-name-register","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/FrameworkContentElement.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/FrameworkContentElement.cs#L53-L89","documentation":"FrameworkContentElement.RegisterName needs the element to be inside a namescope (FindNameScope returns non-null) to record a name/object pair. If no NameScope can be found, InvalidOperationException with NameScopeNotFound(\"register\") is thrown. Namescopes exist only for elements in a loaded XAML tree with a registration context.","triggerScenarios":"Calling RegisterName(name, element) on a FrameworkContentElement that is not part of a parsed XAML tree / has no NameScope (e.g. a free-floating element created in code without a scope).","commonSituations":"Programmatically creating elements (e.g. FlowDocument paragraphs) outside any XAML load and calling RegisterName; calling before the element is attached to a page/document that establishes a namescope; templated content with x:Name issues.","solutions":["Ensure the element is attached to a tree with a NameScope before calling RegisterName.","Create and assign a NameScope explicitly: NameScope.SetNameScope(element, new NameScope()).","Use NameScope.GetNameScope(element) in a guard to check a scope exists first."],"exampleFix":"// before\npara.RegisterName(\"p1\", para); // no scope yet\n// after\nif (NameScope.GetNameScope(para) == null)\n    NameScope.SetNameScope(para, new NameScope());\npara.RegisterName(\"p1\", para);","handlingStrategy":"type-guard","validationCode":"if (NameScope.GetNameScope(element) == null) NameScope.SetNameScope(element, new NameScope());\nelement.RegisterName(name, scopedElement);","typeGuard":"static bool HasNameScope(FrameworkContentElement e) => NameScope.GetNameScope(e) != null;","tryCatchPattern":"try { element.RegisterName(name, obj); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"register\")) { NameScope.SetNameScope(element, new NameScope()); element.RegisterName(name, obj); }","preventionTips":["Register names only after the element is attached to a parsed XAML tree.","Guard with NameScope.GetNameScope before any name registration.","Create a NameScope explicitly for code-built trees."],"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-21T21:30:21.729Z"}