dotnet/wpf · error · InvalidOperationException

SR.NameScopeNotFound (register)

Error message

SR.NameScopeNotFound (register)

What it means

FrameworkElement.RegisterName looks up the INameScope the element belongs to; if no NameScope can be found (FindNameScope returns null), it throws InvalidOperationException with SR.NameScopeNotFound, with 'register' as context. Names can only be registered when the element participates in a namescope (its own or an ancestor's).

Solutions

  1. Attach the element to a rooted tree (Window/Page/NameScope) before calling RegisterName
  2. Explicitly set a namescope: NameScope.SetNameScope(element, new NameScope())
  3. Use the NameScope of the intended container: ((INameScope)container).RegisterName(...)

Example fix

// before
textBox.RegisterName("tb", textBox); // no namescope yet
// after
NameScope.SetNameScope(root, new NameScope());
root.RegisterName("tb", textBox);
Defensive patterns

Strategy: validation

Validate before calling

if (element.FindNameScope() == null) throw new InvalidOperationException("Element has no NameScope; attach it to a rooted tree or set one via NameScope.SetNameScope before RegisterName.");

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling element.RegisterName(name, scopedElement) when the element is not inside any NameScope — e.g. not registered in a XAML page/template namescope and having no NamescopeProperty ancestor chain.

Common situations: Programmatically creating elements and calling RegisterName before attaching them to a rooted visual/logical tree; operating on detached elements from templates; setting up names outside a XamlReader load.

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

Appendix: source

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

                                        }
                                    }

                                    /// <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)