{"record":{"id":"a3f07b6cb2a7b563","repo":"dotnet/wpf","slug":"sr-format-sr-namescopenotfound-name-unregister-a3f07b","errorCode":null,"errorMessage":"SR.Format(SR.NameScopeNotFound, name, \"unregister\")","messagePattern":"SR\\.Format\\(SR\\.NameScopeNotFound, name, \"unregister\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs","lineNumber":89,"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":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs#L71-L107","documentation":"FrameworkElement.UnregisterName looks up the INameScope the element belongs to via FindNameScope(). When no namescope is found it throws InvalidOperationException with the element, the name, and the 'unregister' operation, because there is no scope from which the name could be removed.","triggerScenarios":"Calling element.UnregisterName(name) on an element that is not part of any XAML namescope (FindNameScope() returns null) — typically a detached element or one removed from the tree before unregistering.","commonSituations":"Cleanup code that unregisters names after the element has already been removed from the visual tree; code-generated elements never attached to a page/window; unregistering on a child instead of the scope-owning root.","solutions":["Call UnregisterName on the scope-owning root (Window/Page) while the element is still in the tree.","Guard the call: verify FindNameScope() (or the root's namescope) is non-null before unregistering, or catch InvalidOperationException.","Restructure cleanup so names are unregistered before removing the element from the tree."],"exampleFix":"// before\nroot.Children.Remove(tb);\ntb.UnregisterName(\"tb1\"); // throws: element detached, no namescope\n\n// after\nroot.UnregisterName(\"tb1\"); // unregister via owning scope\nroot.Children.Remove(tb);","handlingStrategy":"validation","validationCode":"if (element.FindNameScope() == null)\n    return; // nothing to unregister from\nelement.UnregisterName(name);","typeGuard":"bool CanUnregisterName(FrameworkElement e) => e.FindNameScope() != null;","tryCatchPattern":"try\n{\n    element.UnregisterName(name);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"unregister\"))\n{\n    Log.Warn($\"No NameScope found when unregistering '{name}'.\", ex);\n}","preventionTips":["Unregister names while the element is still attached to the tree, before removing it.","Call UnregisterName on the scope-owning root rather than detached children.","Make cleanup idempotent: check for the namescope 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"}