dotnet/wpf · error · InvalidOperationException

SR.Format(SR.NameScopeNotFound, name, "unregister")

Error message

SR.Format(SR.NameScopeNotFound, name, "unregister")

What it means

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.

Solutions

  1. Call UnregisterName on the scope-owning root (Window/Page) while the element is still in the tree.
  2. Guard the call: verify FindNameScope() (or the root's namescope) is non-null before unregistering, or catch InvalidOperationException.
  3. Restructure cleanup so names are unregistered before removing the element from the tree.

Example fix

// before
root.Children.Remove(tb);
tb.UnregisterName("tb1"); // throws: element detached, no namescope

// after
root.UnregisterName("tb1"); // unregister via owning scope
root.Children.Remove(tb);
Defensive patterns

Strategy: validation

Validate before calling

if (element.FindNameScope() == null)
    return; // nothing to unregister from
element.UnregisterName(name);

Type guard

bool CanUnregisterName(FrameworkElement e) => e.FindNameScope() != null;

Try / catch

try
{
    element.UnregisterName(name);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("unregister"))
{
    Log.Warn($"No NameScope found when unregistering '{name}'.", ex);
}

Prevention

When it happens

Trigger: 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.

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

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/a3f07b6cb2a7b563. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs:89

                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)