dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

FrameworkContentElement.RegisterName needs the element to be inside a namescope (FindNameScope returns non-null) to record a name/object pair. If no NameScope can be found, InvalidOperationException with NameScopeNotFound("register") is thrown. Namescopes exist only for elements in a loaded XAML tree with a registration context.

Solutions

  1. Ensure the element is attached to a tree with a NameScope before calling RegisterName.
  2. Create and assign a NameScope explicitly: NameScope.SetNameScope(element, new NameScope()).
  3. Use NameScope.GetNameScope(element) in a guard to check a scope exists first.

Example fix

// before
para.RegisterName("p1", para); // no scope yet
// after
if (NameScope.GetNameScope(para) == null)
    NameScope.SetNameScope(para, new NameScope());
para.RegisterName("p1", para);
Defensive patterns

Strategy: type-guard

Validate before calling

if (NameScope.GetNameScope(element) == null) NameScope.SetNameScope(element, new NameScope());
element.RegisterName(name, scopedElement);

Type guard

static bool HasNameScope(FrameworkContentElement e) => NameScope.GetNameScope(e) != null;

Try / catch

try { element.RegisterName(name, obj); } catch (InvalidOperationException ex) when (ex.Message.Contains("register")) { NameScope.SetNameScope(element, new NameScope()); element.RegisterName(name, obj); }

Prevention

When it happens

Trigger: Calling RegisterName(name, element) on a FrameworkContentElement that is not part of a parsed XAML tree / has no NameScope (e.g. a free-floating element created in code without a scope).

Common situations: Programmatically creating elements (e.g. FlowDocument paragraphs) outside any XAML load and calling RegisterName; calling before the element is attached to a page/document that establishes a namescope; templated content with x:Name issues.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs:71

            }
        }

        /// <summary>
        /// Registers the name - element combination from the
        /// NameScope that the current element belongs to.
        /// </summary>
        /// <param name="name">Name of the element</param>
        /// <param name="scopedElement">Element where name is defined</param>
        public void RegisterName(string name, object scopedElement)
        {
            INameScope nameScope = FrameworkElement.FindScope(this);
            if (nameScope != null)
            {
                nameScope.RegisterName(name, scopedElement);
            }
            else
            {
                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"));

View on GitHub (pinned to 81131a70a4)