{"record":{"id":"bf000b77e9025cf4","repo":"dotnet/wpf","slug":"sr-templatechildindexoutofrange-frameworkelement","errorCode":null,"errorMessage":"SR.TemplateChildIndexOutOfRange","messagePattern":"SR\\.TemplateChildIndexOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs","lineNumber":6238,"sourceCode":"            get\n            {\n                uint childIndex = (((uint)_flags2) & 0xFFFF);\n                if (childIndex == 0xFFFF)\n                {\n                    return -1;\n                }\n                else\n                {\n                    return (int)childIndex;\n                }\n            }\n            set\n            {\n                // We store TemplateChildIndex as a 16-bit integer with 0xFFFF meaning \"-1\".\n                // Thus we support any indices in the range [-1, 65535).\n                if (value < -1 || value >= 0xFFFF)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(value), SR.TemplateChildIndexOutOfRange);\n                }\n\n                uint childIndex = (value == -1) ? 0xFFFF : (uint)value;\n\n                _flags2 = (InternalFlags2)(childIndex | (((uint)_flags2) & 0xFFFF0000));\n            }\n        }\n\n        internal bool IsRequestingExpression\n        {\n            get { return ReadInternalFlag2(InternalFlags2.IsRequestingExpression); }\n            set { WriteInternalFlag2(InternalFlags2.IsRequestingExpression, value); }\n        }\n\n        internal bool BypassLayoutPolicies\n        {\n            get { return ReadInternalFlag2(InternalFlags2.BypassLayoutPolicies); }\n            set { WriteInternalFlag2(InternalFlags2.BypassLayoutPolicies, value); }","sourceCodeStart":6220,"sourceCodeEnd":6256,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L6220-L6256","documentation":"TemplateChildIndex is stored internally as a 16-bit value where 0xFFFF encodes -1, so legal values are [-1, 65535]. The internal setter validates this and throws ArgumentOutOfRangeException(SR.TemplateChildIndexOutOfRange) for anything outside that range.","triggerScenarios":"Internal/template infrastructure assigning TemplateChildIndex values >= 0xFFFF or < -1, typically when a compiled template contains more than 65534 template children or a corrupt index is computed.","commonSituations":"Extremely large generated templates (thousands of nodes) from code generation hitting the 16-bit cap; low-level template tooling or reflection-based code writing the index directly.","solutions":["Reduce the number of nodes in the offending template below the 16-bit limit (split into nested templates/user controls)","Regenerate the template if it was emitted by a tool producing excessive nodes","Avoid writing TemplateChildIndex directly; let the framework assign it during template compilation","If you own the generator, cap output size or emit nested FrameworkElementFactory hierarchies"],"exampleFix":"// before\n// one generated template with 70000 nodes\n// after\n// split generation into StackPanel with child user controls, each < 65534 nodes","handlingStrategy":"validation","validationCode":"if (childIndex < -1 || childIndex >= 0xFFFF) throw new ArgumentOutOfRangeException(nameof(childIndex));","typeGuard":"bool IsValidTemplateChildIndex(int i) => i is >= -1 and < 0xFFFF;","tryCatchPattern":"try { SetTemplateChildIndex(node, idx); }\ncatch (ArgumentOutOfRangeException) { /* template too large; split it */ }","preventionTips":["Keep generated templates well under 65k nodes","Never write TemplateChildIndex manually","Split huge generated UIs into nested controls"],"tags":["wpf","templates","argument-out-of-range"],"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-22T01:17:13.364Z"}