{"record":{"id":"39c6ea5b4516f00c","repo":"dotnet/wpf","slug":"sr-namescopeduplicatenamesnotallowed-namescope","errorCode":null,"errorMessage":"SR.NameScopeDuplicateNamesNotAllowed","messagePattern":"SR\\.NameScopeDuplicateNamesNotAllowed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs","lineNumber":64,"sourceCode":"                throw new ArgumentException(SR.Format(SR.NameScopeInvalidIdentifierName, name));\n            }\n\n            if (_nameMap == null)\n            {\n                _nameMap = new HybridDictionary();\n                _nameMap[name] = scopedElement;\n            }\n            else\n            {\n                object nameContext = _nameMap[name];\n                // first time adding the Name, set it\n                if (nameContext == 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            if (TraceNameScope.IsEnabled)\n            {\n                TraceNameScope.TraceActivityItem( TraceNameScope.RegisterName,\n                                                  this, \n                                                  name,\n                                                  scopedElement );\n            }\n        }\n\n        /// <summary>\n        /// Unregister Name-Object Map \n        /// </summary>\n        /// <param name=\"name\">name to be registered</param>\n        public void UnregisterName(string name)\n        {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs#L46-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","solutions":["Check the existing registration first (FindName(name)) and only register when null or when the existing element equals the new one.","UnregisterName(name) before registering the new element if replacement is intended.","Ensure generated/templated names are unique (append a counter or GUID).","Catch ArgumentException to detect and resolve collisions at load time."],"exampleFix":"// before\nscope.RegisterName(\"item\", newItem); // \"item\" already maps to oldItem\n// after\nvar existing = scope.FindName(\"item\");\nif (existing == null)\n    scope.RegisterName(\"item\", newItem);\nelse if (existing != newItem)\n{\n    scope.UnregisterName(\"item\");\n    scope.RegisterName(\"item\", newItem);\n}","handlingStrategy":"validation","validationCode":"bool free = scope.FindName(name) == null || ReferenceEquals(scope.FindName(name), element);","typeGuard":"null","tryCatchPattern":"try { scope.RegisterName(name, element); }\ncatch (ArgumentException) { /* name already registered to a different element */ }","preventionTips":["Check FindName before registering a possibly-colliding name.","Make generated element names unique (suffix with a counter).","Unregister the old mapping first when intentional replacement is needed."],"tags":["wpf","xaml","namescope","duplicate-name"],"backgroundTag":"file-already-exists","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}