dotnet/wpf · error · ArgumentException

SR.NameScopeDuplicateNamesNotAllowed

Error message

SR.NameScopeDuplicateNamesNotAllowed

What it means

NameScope.RegisterName throws ArgumentException(SR.NameScopeDuplicateNamesNotAllowed) when registering a name that is already mapped to a DIFFERENT element. Re-registering the same name for the same element is allowed (no-op); only a conflicting element triggers the throw. Names within a namescope must be unique.

Solutions

  1. Check the existing registration first (FindName(name)) and only register when null or when the existing element equals the new one.
  2. UnregisterName(name) before registering the new element if replacement is intended.
  3. Ensure generated/templated names are unique (append a counter or GUID).
  4. Catch ArgumentException to detect and resolve collisions at load time.

Example fix

// before
scope.RegisterName("item", newItem); // "item" already maps to oldItem
// after
var existing = scope.FindName("item");
if (existing == null)
    scope.RegisterName("item", newItem);
else if (existing != newItem)
{
    scope.UnregisterName("item");
    scope.RegisterName("item", newItem);
}
Defensive patterns

Strategy: validation

Validate before calling

bool free = scope.FindName(name) == null || ReferenceEquals(scope.FindName(name), element);

Type guard

null

Try / catch

try { scope.RegisterName(name, element); }
catch (ArgumentException) { /* name already registered to a different element */ }

Prevention

When it happens

Trigger: Calling RegisterName(name, elementB) when name is already registered for elementA (elementA != elementB); programmatically adding children with identical names into the same namescope; loading XAML fragments whose names collide with existing elements.

Common situations: Cloning/importing element trees into an existing namescope; template or item-generation code that reuses hard-coded names; merging user controls that both define a name like 'root'.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/39c6ea5b4516f00c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs:64

                throw new ArgumentException(SR.Format(SR.NameScopeInvalidIdentifierName, name));
            }

            if (_nameMap == null)
            {
                _nameMap = new HybridDictionary();
                _nameMap[name] = scopedElement;
            }
            else
            {
                object nameContext = _nameMap[name];
                // first time adding the Name, set it
                if (nameContext == null)
                {
                    _nameMap[name] = scopedElement;
                }
                else if (scopedElement != nameContext)
                {
                    throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, name));
                }   
            }

            if (TraceNameScope.IsEnabled)
            {
                TraceNameScope.TraceActivityItem( TraceNameScope.RegisterName,
                                                  this, 
                                                  name,
                                                  scopedElement );
            }
        }

        /// <summary>
        /// Unregister Name-Object Map 
        /// </summary>
        /// <param name="name">name to be registered</param>
        public void UnregisterName(string name)
        {

View on GitHub (pinned to 81131a70a4)