{"record":{"id":"a8ad132ecebcba39","repo":"dotnet/wpf","slug":"sr-namescopenotfound-unregister","errorCode":null,"errorMessage":"SR.NameScopeNotFound (unregister)","messagePattern":"SR\\.NameScopeNotFound \\(unregister\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs","lineNumber":153,"sourceCode":"                                            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\"));\n                                        }\n                                    }\n\n                                    /// <summary>\n                                    /// Find the object with given name in the\n                                    /// NameScope that the current element belongs to.\n                                    /// </summary>\n                                    /// <param name=\"name\">string name to index</param>\n                                    /// <returns>context if found, else null</returns>\n                                    public object FindName(string name)\n                                    {\n                                        DependencyObject scopeOwner;\n                                        return FindName(name, out scopeOwner);\n                                    }\n\n                                    // internal version of FindName that returns the scope owner\n                                    internal object FindName(string name, out DependencyObject scopeOwner)\n                                    {","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs#L135-L171","documentation":"FrameworkElement.UnregisterName requires the element to belong to an INameScope; FindNameScope returning null causes InvalidOperationException(SR.NameScopeNotFound) with context 'unregister'. Without a namescope there is no registration table from which the name can be removed.","triggerScenarios":"Calling element.UnregisterName(name) on a detached element or one whose ancestor chain has no NameScope (no NamescopeProperty set on self or ancestors).","commonSituations":"Cleanup code running after the element was removed from the tree; unregistering names on elements created purely in code; teardown after a templated parent was cleared.","solutions":["Ensure the element is still attached to the namescope that registered the name before unregistering","Capture the INameScope at registration time and call UnregisterName on it directly","Guard with element.FindNameScope() != null before calling UnregisterName"],"exampleFix":"// before\nchild.UnregisterName(\"ok\"); // detached from page\n// after\nvar scope = (INameScope)NameScope.GetNameScope(page);\nscope?.UnregisterName(\"ok\");","handlingStrategy":"validation","validationCode":"if (element.FindNameScope() == null) return; // nothing registered in a namescope; skip unregister","typeGuard":"bool HasNameScope(System.Windows.DependencyObject e) => System.Windows.NameScope.GetNameScope(e) != null;","tryCatchPattern":"try { el.UnregisterName(name); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"NameScope\")) { /* element detached; resolve via original scope reference */ }","preventionTips":["Unregister names while the element is still attached to its namescope","Keep a reference to the INameScope used at registration","Check FindNameScope() != null before unregistering"],"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"}