{"record":{"id":"b28324b480ad98fd","repo":"dotnet/wpf","slug":"sr-format-sr-namescopeduplicatenamesnotallowed-childname","errorCode":null,"errorMessage":"SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)","messagePattern":"SR\\.Format\\(SR\\.NameScopeDuplicateNamesNotAllowed, childName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":5316,"sourceCode":"        internal static int CreateChildIndexFromChildName(\n            string              childName,\n            FrameworkTemplate   frameworkTemplate)\n        {\n            Debug.Assert(frameworkTemplate != null );\n\n            // Lock must be made by caller\n\n            HybridDictionary childIndexFromChildName;\n            int lastChildIndex;\n\n            Debug.Assert(frameworkTemplate != null);\n\n            childIndexFromChildName = frameworkTemplate.ChildIndexFromChildName;\n            lastChildIndex = frameworkTemplate.LastChildIndex;\n\n            if (childIndexFromChildName.Contains(childName))\n            {\n                throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName));\n            }\n\n            // Normal templated child check\n            // If we're about to give out an index that we can't support, throw.\n            if (lastChildIndex >= 0xFFFF)\n            {\n                throw new InvalidOperationException(SR.StyleHasTooManyElements);\n            }\n\n            // No index found, allocate\n            object value = lastChildIndex;\n\n            childIndexFromChildName[childName] = value;\n\n            Interlocked.Increment(ref lastChildIndex);\n\n            Debug.Assert(frameworkTemplate != null);\n            frameworkTemplate.LastChildIndex = lastChildIndex;","sourceCodeStart":5298,"sourceCodeEnd":5334,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L5298-L5334","documentation":"When registering a template child name (AddTemplatedChildName-style registration), the name already exists in the template's childIndexFromChildName map. Duplicate x:Name values within one template are illegal, so ArgumentException (NameScopeDuplicateNamesNotAllowed) is thrown — mirroring NameScope duplicate-name semantics.","triggerScenarios":"Two elements in the same ControlTemplate/DataTemplate sharing the same x:Name; re-registering an already-registered child name during template optimization/BAML processing; merging template fragments that reuse names.","commonSituations":"Copy-pasted XAML fragments inside one template with repeated names; programmatic template building calling the registration API twice for the same name; tooling-generated XAML that doesn't deduplicate names.","solutions":["Make every x:Name unique within the template (e.g. prefix per element)","Remove the duplicate element or its name attribute if the name isn't referenced","Uniquify names programmatically before registering: check childIndexFromChildName.Contains first","Run an XAML linter to detect duplicate names in templates"],"exampleFix":"<!-- before -->\n<Border x:Name=\"panel\"/><TextBlock x:Name=\"panel\"/>\n<!-- after -->\n<Border x:Name=\"panelBorder\"/><TextBlock x:Name=\"panelText\"/>","handlingStrategy":"validation","validationCode":"var seen = new HashSet<string>();\nforeach (var name in CollectXNames(template))\n    if (!seen.Add(name)) throw new Exception(\"Duplicate x:Name in template: \" + name);","typeGuard":null,"tryCatchPattern":"try { ApplyTemplate(); } catch (ArgumentException ex) when (ex.Message.Contains(\"name\")) { Log(\"Duplicate template name: \" + ex.Message); }","preventionTips":["Enforce unique x:Name per template in code review/lint","Uniquify generated names with prefixes or counters","Search template XAML for duplicated Name attributes before build"],"tags":["wpf","xaml","namescope","duplicate"],"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"}