{"record":{"id":"e03e1bdf1d02a2ca","repo":"dotnet/wpf","slug":"sr-namescopeduplicatenamesnotallowed-namescopedictionary","errorCode":null,"errorMessage":"SR.NameScopeDuplicateNamesNotAllowed","messagePattern":"SR\\.NameScopeDuplicateNamesNotAllowed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs","lineNumber":72,"sourceCode":"            }\n            else\n            {\n                if (_nameMap is null)\n                {\n                    _nameMap = new HybridDictionary();\n                    _nameMap[name] = scopedElement;\n                }\n                else\n                {\n                    object nameContext = _nameMap[name];\n\n                    if (nameContext is null)\n                    {\n                        _nameMap[name] = scopedElement;\n                    }\n                    else if (scopedElement != nameContext)\n                    {\n                        throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, name));\n                    }\n                }\n            }\n        }\n\n        public void UnregisterName(string name)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            if (name.Length == 0)\n                throw new ArgumentException(SR.NameScopeNameNotEmptyString);\n\n            if (_underlyingNameScope is not null)\n            {\n                _underlyingNameScope.UnregisterName(name);\n                _names.Remove(name);\n            }\n            else","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["UnregisterName the existing name first if reassignment is intended","Ensure names are unique per scope (append an index/suffix)","Check FindName(name) and compare the existing element before registering"],"exampleFix":"// before\nnameScopeDictionary.RegisterName(\"Panel\", newPanel); // \"Panel\" maps to oldPanel\n// after\nobject existing = nameScopeDictionary.FindName(\"Panel\");\nif (existing != null && existing != newPanel) nameScopeDictionary.UnregisterName(\"Panel\");\nnameScopeDictionary.RegisterName(\"Panel\", newPanel);","handlingStrategy":"validation","validationCode":"object existing = nameScopeDictionary.FindName(name);\nif (existing is not null && !ReferenceEquals(existing, element)) nameScopeDictionary.UnregisterName(name);\nnameScopeDictionary.RegisterName(name, element);","typeGuard":"static bool IsFreeOrSame(NameScopeDictionary d, string n, object e) { var x = d.FindName(n); return x is null || ReferenceEquals(x, e); }","tryCatchPattern":"try { nameScopeDictionary.RegisterName(name, element); }\ncatch (ArgumentException ex) when (ex.Message.Contains(name)) { /* duplicate name for different element */ }","preventionTips":["Uniquify names per scope (suffix with index)","Unregister before re-registering a name to a new element","Avoid hard-coded names inside loops or templates"],"tags":["wpf","system-xaml","namescope","duplicate-name","argumentexception"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}