dotnet/wpf · error · ArgumentException
SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
Error message
SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
What it means
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.
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
Example fix
<!-- before --> <Border x:Name="panel"/><TextBlock x:Name="panel"/> <!-- after --> <Border x:Name="panelBorder"/><TextBlock x:Name="panelText"/>
Defensive patterns
Strategy: validation
Validate before calling
var seen = new HashSet<string>();
foreach (var name in CollectXNames(template))
if (!seen.Add(name)) throw new Exception("Duplicate x:Name in template: " + name); Try / catch
try { ApplyTemplate(); } catch (ArgumentException ex) when (ex.Message.Contains("name")) { Log("Duplicate template name: " + ex.Message); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- SR.Format(SR.NameScopeNotFound, name, "register")
- SR.Format(SR.NameScopeNotFound, name, "register")
- SR.Format(SR.NameScopeNotFound, name, "unregister")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b28324b480ad98fd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs:5316
internal static int CreateChildIndexFromChildName(
string childName,
FrameworkTemplate frameworkTemplate)
{
Debug.Assert(frameworkTemplate != null );
// Lock must be made by caller
HybridDictionary childIndexFromChildName;
int lastChildIndex;
Debug.Assert(frameworkTemplate != null);
childIndexFromChildName = frameworkTemplate.ChildIndexFromChildName;
lastChildIndex = frameworkTemplate.LastChildIndex;
if (childIndexFromChildName.Contains(childName))
{
throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName));
}
// Normal templated child check
// If we're about to give out an index that we can't support, throw.
if (lastChildIndex >= 0xFFFF)
{
throw new InvalidOperationException(SR.StyleHasTooManyElements);
}
// No index found, allocate
object value = lastChildIndex;
childIndexFromChildName[childName] = value;
Interlocked.Increment(ref lastChildIndex);
Debug.Assert(frameworkTemplate != null);
frameworkTemplate.LastChildIndex = lastChildIndex;View on GitHub (pinned to 81131a70a4)