{"record":{"id":"b6c572a696b96974","repo":"dotnet/wpf","slug":"sr-stylehastoomanyelements","errorCode":null,"errorMessage":"SR.StyleHasTooManyElements","messagePattern":"SR\\.StyleHasTooManyElements","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":5323,"sourceCode":"\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;\n\n            return (int)value;\n        }\n\n        //\n        //  This method\n        //  1. Returns the ChildIndex for the given ChildName. If there isn't an","sourceCodeStart":5305,"sourceCodeEnd":5341,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L5305-L5341","documentation":"Template child indices are stored in a 16-bit value; when a template's total number of named children would exceed 0xFFFF, WPF throws InvalidOperationException (StyleHasTooManyElements) instead of allocating an index that cannot be represented.","triggerScenarios":"Registering a templated child name when frameworkTemplate.LastChildIndex >= 0xFFFF — i.e. a template with more than 65535 named children; unbounded programmatic child registration loops inside one template.","commonSituations":"Code-generated templates with enormous numbers of named children; pathological XAML generated from data with a name per item; flattened designs that give every descendant an x:Name.","solutions":["Reduce the number of named elements in the template (name only elements referenced by triggers/storyboards)","Split into nested templates or use ItemsControl for repeated children instead of naming each","Remove x:Name from decorative children; rely on visual-tree traversal instead","Generate multiple templates so each stays under the index limit"],"exampleFix":"// before\nfor (int i = 0; i < 70000; i++)\n    StyleHelper.RegisterName(template, \"child\" + i, fe); // exceeds 0xFFFF\n// after\nconst int MaxNamedChildren = 0xFFFF;\nfor (int i = 0; i < Math.Min(count, MaxNamedChildren); i++)\n    StyleHelper.RegisterName(template, \"child\" + i, fe);","handlingStrategy":"validation","validationCode":"static bool WithinIndexLimit(int namedChildren) => namedChildren <= 0xFFFF;","typeGuard":null,"tryCatchPattern":"try { ApplyTemplate(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"too many\")) { Log(\"Template exceeds 65535 named children\"); UseLightweightTemplate(); }","preventionTips":["Name only elements that triggers/storyboards reference","Use ItemsControl/virtualization for large repeated content","Split giant templates into composed smaller templates","Assert named-child count < 0xFFFF in template generation code"],"tags":["wpf","templates","limits","capacity"],"backgroundTag":"value-out-of-range","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"}