dotnet/wpf · error · InvalidOperationException

SR.NameScopeNotFound (unregister)

Error message

SR.NameScopeNotFound (unregister)

What it means

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.

Solutions

  1. Ensure the element is still attached to the namescope that registered the name before unregistering
  2. Capture the INameScope at registration time and call UnregisterName on it directly
  3. Guard with element.FindNameScope() != null before calling UnregisterName

Example fix

// before
child.UnregisterName("ok"); // detached from page
// after
var scope = (INameScope)NameScope.GetNameScope(page);
scope?.UnregisterName("ok");
Defensive patterns

Strategy: validation

Validate before calling

if (element.FindNameScope() == null) return; // nothing registered in a namescope; skip unregister

Type guard

bool HasNameScope(System.Windows.DependencyObject e) => System.Windows.NameScope.GetNameScope(e) != null;

Try / catch

try { el.UnregisterName(name); } catch (InvalidOperationException ex) when (ex.Message.Contains("NameScope")) { /* element detached; resolve via original scope reference */ }

Prevention

When it happens

Trigger: Calling element.UnregisterName(name) on a detached element or one whose ancestor chain has no NameScope (no NamescopeProperty set on self or ancestors).

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a8ad132ecebcba39. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs:153

                                            throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, "register"));
                                        }
                                    }

                                    /// <summary>
                                    /// Unregisters the name - element combination from the
                                    /// NameScope that the current element belongs to.
                                    /// </summary>
                                    /// <param name="name">Name of the element</param>
                                    public void UnregisterName(string name)
                                    {
                                        INameScope nameScope = FrameworkElement.FindScope(this);
                                        if (nameScope != null)
                                        {
                                            nameScope.UnregisterName(name);
                                        }
                                        else
                                        {
                                            throw new InvalidOperationException(SR.Format(SR.NameScopeNotFound, name, "unregister"));
                                        }
                                    }

                                    /// <summary>
                                    /// Find the object with given name in the
                                    /// NameScope that the current element belongs to.
                                    /// </summary>
                                    /// <param name="name">string name to index</param>
                                    /// <returns>context if found, else null</returns>
                                    public object FindName(string name)
                                    {
                                        DependencyObject scopeOwner;
                                        return FindName(name, out scopeOwner);
                                    }

                                    // internal version of FindName that returns the scope owner
                                    internal object FindName(string name, out DependencyObject scopeOwner)
                                    {

View on GitHub (pinned to 81131a70a4)