dotnet/wpf · error · ArgumentException
SR.NameScopeNameNotFound
Error message
SR.NameScopeNameNotFound
What it means
NameScope.UnregisterName removes a name-to-object mapping from a XAML namescope. If the name does not exist in the namescope's name map, this ArgumentException is thrown. The library treats unregistering an unknown name as a programming error rather than silently ignoring it.
Solutions
- Call NameScope.ContainsName(nameScope, name) (or check FindName != null) before calling UnregisterName.
- Wrap UnregisterName in try/catch for ArgumentException if double-unregister is expected in your cleanup logic.
- Verify you are operating on the correct namescope instance — names registered on an ancestor namescope are not visible here.
- Check that the name string exactly matches the one passed to RegisterName (case-sensitive).
Example fix
// before
nameScope.UnregisterName("okButton"); // throws if never registered
// after
if (NameScope.ContainsName(nameScope, "okButton"))
{
nameScope.UnregisterName("okButton");
} Defensive patterns
Strategy: validation
Validate before calling
if (NameScope.ContainsName(nameScope, name))
{
nameScope.UnregisterName(name);
} Try / catch
try
{
nameScope.UnregisterName(name);
}
catch (ArgumentException ex) when (ex.Message.Contains("NameScopeNameNotFound"))
{
// name was not registered; safe to ignore during cleanup
} Prevention
- Mirror every RegisterName with exactly one UnregisterName in symmetric setup/teardown code.
- Use ContainsName checks in shared cleanup helpers.
- Remember names are case-sensitive and namescope-specific.
When it happens
Trigger: Calling UnregisterName(name) on a NameScope (or element via NameScope.SetNameScope/FindName-related helpers) where `name` was never registered with RegisterName, or was already unregistered, or the namescope is empty.
Common situations: Unregistering a XAML element name twice; unregistering a name that was defined in a different namescope (e.g. templates vs page); trying to remove a name registered programmatically with a different casing; cleanup code running before registrations occurred.
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.CanOnlyHaveOneChild
- SR.CanOnlyHaveOneChild
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/74734bb9b9724c3d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs:94
/// <summary>
/// Unregister Name-Object Map
/// </summary>
/// <param name="name">name to be registered</param>
public void UnregisterName(string name)
{
ArgumentNullException.ThrowIfNull(name);
if (name.Length == 0)
throw new ArgumentException(SR.NameScopeNameNotEmptyString);
if (_nameMap != null && _nameMap[name] != null)
{
_nameMap.Remove(name);
}
else
{
throw new ArgumentException(SR.Format(SR.NameScopeNameNotFound, name));
}
if (TraceNameScope.IsEnabled)
{
TraceNameScope.TraceActivityItem( TraceNameScope.UnregisterName,
this, name );
}
}
/// <summary>
/// Find - Find the corresponding object given a Name
/// </summary>
/// <param name="name">Name for which context needs to be retrieved</param>
/// <returns>corresponding Context if found, else null</returns>
public object FindName(string name)
{
if (_nameMap == null || string.IsNullOrEmpty(name))
return null;View on GitHub (pinned to 81131a70a4)