dotnet/wpf · error · InvalidOperationException
SR.Format(SR.NameScopeNotFound, name, "register")
Error message
SR.Format(SR.NameScopeNotFound, name, "register")
What it means
FrameworkElement.RegisterName requires the element to be inside a name scope (usually established by an XAML page/template). FindNameScope() returned null, so there is no INameScope in which to register the name, and WPF throws InvalidOperationException naming the element and the 'register' operation.
Solutions
- Add the element to its parent tree (Window, Page, Control template content) before calling RegisterName so a NameScope exists.
- Register names on the root element that owns the namescope (e.g. window.RegisterName(...)) instead of a detached child.
- Alternatively use NameScope.SetNameScope(element, new NameScope()) to attach a scope manually for code-only trees.
Example fix
// before
var tb = new TextBlock { Name = "tb1" };
tb.RegisterName("tb1", tb); // throws: no namescope yet
root.Children.Add(tb);
// after
var tb = new TextBlock { Name = "tb1" };
root.Children.Add(tb); // attach first -> namescope available
root.RegisterName("tb1", tb); Defensive patterns
Strategy: validation
Validate before calling
if (element.FindNameScope() == null)
throw new InvalidOperationException("Element is not in a NameScope; attach it to the tree first.");
element.RegisterName(name, scopedElement); Type guard
bool CanRegisterName(FrameworkElement e) => e.FindNameScope() != null;
Try / catch
try
{
element.RegisterName(name, scopedElement);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("register"))
{
Log.Warn($"No NameScope for '{name}'; element not attached to tree.", ex);
} Prevention
- Register names only after the element is added to a Window/Page/control tree.
- Use the scope-owning root (Window/Page) for RegisterName calls in code-only trees.
- For detached trees, attach a scope explicitly with NameScope.SetNameScope.
When it happens
Trigger: Calling element.RegisterName(name, scopedElement) on a FrameworkElement whose FindNameScope() is null — i.e. the element is not yet attached to a name-scoped tree (e.g. not added to a Window/Page that defines a NameScope).
Common situations: Registering names on a freshly constructed element created in code before adding it to the visual/logical tree; calling RegisterName during construction or before the element is loaded; elements outside any XAML namescope such as free-floating controls.
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
- SR.Format(SR.NameScopeNotFound, name, "register")
- SR.Format(SR.NameScopeNotFound, name, "unregister")
- SR.NameScopeNotFound (register)
- SR.NameScopeNotFound (unregister)
- (no message - parameterless InvalidOperationException)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/83955d6140e4d5e5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.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)