{"record":{"id":"0c9fd0a1f793999d","repo":"dotnet/wpf","slug":"sr-namescopenotfound-register","errorCode":null,"errorMessage":"SR.NameScopeNotFound (register)","messagePattern":"SR\\.NameScopeNotFound \\(register\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs","lineNumber":135,"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":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs#L117-L153","documentation":"FrameworkElement.RegisterName looks up the INameScope the element belongs to; if no NameScope can be found (FindNameScope returns null), it throws InvalidOperationException with SR.NameScopeNotFound, with 'register' as context. Names can only be registered when the element participates in a namescope (its own or an ancestor's).","triggerScenarios":"Calling element.RegisterName(name, scopedElement) when the element is not inside any NameScope — e.g. not registered in a XAML page/template namescope and having no NamescopeProperty ancestor chain.","commonSituations":"Programmatically creating elements and calling RegisterName before attaching them to a rooted visual/logical tree; operating on detached elements from templates; setting up names outside a XamlReader load.","solutions":["Attach the element to a rooted tree (Window/Page/NameScope) before calling RegisterName","Explicitly set a namescope: NameScope.SetNameScope(element, new NameScope())","Use the NameScope of the intended container: ((INameScope)container).RegisterName(...)"],"exampleFix":"// before\ntextBox.RegisterName(\"tb\", textBox); // no namescope yet\n// after\nNameScope.SetNameScope(root, new NameScope());\nroot.RegisterName(\"tb\", textBox);","handlingStrategy":"validation","validationCode":"if (element.FindNameScope() == null) throw new InvalidOperationException(\"Element has no NameScope; attach it to a rooted tree or set one via NameScope.SetNameScope before RegisterName.\");","typeGuard":"bool HasNameScope(System.Windows.DependencyObject e) => System.Windows.NameScope.GetNameScope(e) != null;","tryCatchPattern":"try { el.RegisterName(name, target); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"NameScope\")) { NameScope.SetNameScope(el, new NameScope()); el.RegisterName(name, target); }","preventionTips":["Register names only after the element is in a rooted XAML tree","Set an explicit NameScope for code-created element trees","Capture and reuse the container's INameScope for name operations"],"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"}