{"record":{"id":"b5ad43c8940e7185","repo":"dotnet/wpf","slug":"sr-templatechildindexoutofrange","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/FrameworkContentElement.cs","lineNumber":2099,"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        // Extracts the required flag and returns\n        // bool to indicate if it is set or unset\n        internal bool ReadInternalFlag(InternalFlags reqFlag)\n        {","sourceCodeStart":2081,"sourceCodeEnd":2117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs#L2081-L2117","documentation":"The internal TemplateChildIndex property on FrameworkContentElement throws ArgumentOutOfRangeException when set to a value below -1 or at/above 0xFFFF (65535). The index is stored in a 16-bit field where 0xFFFF encodes -1 (no template child), so only values in [-1, 65534] are representable.","triggerScenarios":"Assigning TemplateChildIndex = -2, or any value >= 65535, when building/patching template child mappings programmatically.","commonSituations":"Custom template infrastructure or code generators that assign template child indices computed from large item counts; corruption/underflow in index bookkeeping.","solutions":["Clamp or validate the index before assignment: if (idx < -1 || idx >= 0xFFFF) handle the error","Use -1 (not -2 or other sentinels) to mean 'no template child'","If more than 65534 template children are needed, restructure the template - the encoding cannot support it"],"exampleFix":"// before\nel.TemplateChildIndex = childIndexFromList; // throws if childIndexFromList >= 65535\n\n// after\nif (childIndexFromList >= -1 && childIndexFromList < 0xFFFF)\n    el.TemplateChildIndex = childIndexFromList;\nelse\n    el.TemplateChildIndex = -1;","handlingStrategy":"validation","validationCode":"if (index < -1 || index >= 0xFFFF)\n    throw new ArgumentOutOfRangeException(nameof(index));\nel.TemplateChildIndex = index;","typeGuard":"static bool IsValidTemplateChildIndex(int v) => v >= -1 && v < 0xFFFF;","tryCatchPattern":"try { el.TemplateChildIndex = idx; }\ncatch (ArgumentOutOfRangeException ex) { log.Error(\"TemplateChildIndex out of [-1, 65534]\", ex); }","preventionTips":["Use -1 as the only sentinel for 'no template child'","Validate indices computed from dynamic content before assignment","Redesign templates exceeding 65534 children rather than widening indices"],"tags":["wpf","argument-out-of-range","templates"],"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"}