dotnet/wpf · error · ArgumentException

SR.NameScopeDuplicateNamesNotAllowed

Error message

SR.NameScopeDuplicateNamesNotAllowed

What it means

RegisterName throws ArgumentException (SR.NameScopeDuplicateNamesNotAllowed, including the duplicate name) when the name is already registered to a DIFFERENT object. Re-registering the same name for the same object is allowed; conflicting registrations are not.

Solutions

  1. UnregisterName the existing name first if reassignment is intended
  2. Ensure names are unique per scope (append an index/suffix)
  3. Check FindName(name) and compare the existing element before registering

Example fix

// before
nameScopeDictionary.RegisterName("Panel", newPanel); // "Panel" maps to oldPanel
// after
object existing = nameScopeDictionary.FindName("Panel");
if (existing != null && existing != newPanel) nameScopeDictionary.UnregisterName("Panel");
nameScopeDictionary.RegisterName("Panel", newPanel);
Defensive patterns

Strategy: validation

Validate before calling

object existing = nameScopeDictionary.FindName(name);
if (existing is not null && !ReferenceEquals(existing, element)) nameScopeDictionary.UnregisterName(name);
nameScopeDictionary.RegisterName(name, element);

Type guard

static bool IsFreeOrSame(NameScopeDictionary d, string n, object e) { var x = d.FindName(n); return x is null || ReferenceEquals(x, e); }

Try / catch

try { nameScopeDictionary.RegisterName(name, element); }
catch (ArgumentException ex) when (ex.Message.Contains(name)) { /* duplicate name for different element */ }

Prevention

When it happens

Trigger: Calling RegisterName(name, otherElement) where name already maps to a different element in the scope; templates or loops reusing the same name for multiple elements.

Common situations: Dynamically adding controls in a loop with hard-coded names; template instantiation in ItemsControl where each item tries to register the same x:Name; migrating code from frameworks that silently overwrite.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs:72

            }
            else
            {
                if (_nameMap is null)
                {
                    _nameMap = new HybridDictionary();
                    _nameMap[name] = scopedElement;
                }
                else
                {
                    object nameContext = _nameMap[name];

                    if (nameContext is null)
                    {
                        _nameMap[name] = scopedElement;
                    }
                    else if (scopedElement != nameContext)
                    {
                        throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, name));
                    }
                }
            }
        }

        public void UnregisterName(string name)
        {
            ArgumentNullException.ThrowIfNull(name);

            if (name.Length == 0)
                throw new ArgumentException(SR.NameScopeNameNotEmptyString);

            if (_underlyingNameScope is not null)
            {
                _underlyingNameScope.UnregisterName(name);
                _names.Remove(name);
            }
            else

View on GitHub (pinned to 81131a70a4)