dotnet/wpf · error · ArgumentException

SR.NameScopeDuplicateNamesNotAllowed

Error message

SR.NameScopeDuplicateNamesNotAllowed

What it means

NameScope.RegisterName throws ArgumentException(SR.NameScopeDuplicateNamesNotAllowed) when the name is already registered to a DIFFERENT object. Re-registering the same name for the same object is a no-op, but mapping one name to two elements is ambiguous and forbidden.

Solutions

  1. UnregisterName the old registration first if reassignment is intended.
  2. Ensure each registered element gets a unique name (suffix with an index).
  3. Fix duplicated x:Name attributes in the XAML markup.

Example fix

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

Strategy: validation

Validate before calling

var existing = nameScope.FindName(name); if (existing != null && !ReferenceEquals(existing, element)) nameScope.UnregisterName(name);

Try / catch

try { nameScope.RegisterName(name, element); } catch (ArgumentException ex) when (ex.Message.Contains("Duplicate")) { nameScope.UnregisterName(name); nameScope.RegisterName(name, element); }

Prevention

When it happens

Trigger: Calling RegisterName("x", elementA) then RegisterName("x", elementB) on the same NameScope; duplicated x:Name values within one XAML namescope.

Common situations: Copy-pasted XAML elements retaining the same x:Name, programmatic loops registering each item under a constant name, template instantiation into a shared scope.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs:56

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

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

        /// <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?[name] is null)

View on GitHub (pinned to 81131a70a4)