{"record":{"id":"e1d5a04593b54db1","repo":"dotnet/wpf","slug":"sr-childtemplateinstancedoesnotexist","errorCode":null,"errorMessage":"SR.ChildTemplateInstanceDoesNotExist","messagePattern":"SR\\.ChildTemplateInstanceDoesNotExist","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":4086,"sourceCode":"                    DependencyObject child = null;\n                    DependencyProperty invalidProperty = resourceDependents[i].Property;\n\n                    int childIndex = resourceDependents[i].ChildIndex;\n                    if (childIndex == 0)\n                    {\n                        // Index '0' means 'self' (container)\n                        child = container;\n                    }\n                    else if (invalidateVisualTreeToo)\n                    {\n                        Debug.Assert(styledChildren != null, \"Should reach here only if the template tree has already been created\");\n\n                        // Locate child to invalidate\n                        child = GetChild(styledChildren, childIndex);\n\n                        if (child == null)\n                        {\n                            throw new InvalidOperationException(SR.ChildTemplateInstanceDoesNotExist);\n                        }\n                    }\n\n                    if (child != null)\n                    {\n                        // Invalidate property on child\n                        child.InvalidateProperty(invalidProperty);\n\n                        // skip remaining dependents for the same property - we only\n                        // need to invalidate once.  The list is sorted, so we just need\n                        // to skip until we find a new property.\n                        int dpIndex = invalidProperty.GlobalIndex;\n                        while (++i < resourceDependents.Count)\n                        {\n                            if (resourceDependents[i].ChildIndex != childIndex ||\n                                resourceDependents[i].Property.GlobalIndex != dpIndex)\n                            {\n                                break;","sourceCodeStart":4068,"sourceCodeEnd":4104,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L4068-L4104","documentation":"While invalidating a property on a style-created child (InvalidatePropertyOnTemplateFetch-style path), GetChild returned null for the requested childIndex, meaning no template instance exists under that index. WPF throws InvalidOperationException (ChildTemplateInstanceDoesNotExist) because a property cannot be pushed to a child that was never instantiated.","triggerScenarios":"A Style value/trigger change targeting childIndex whose element was never created (template not yet applied, child conditionally removed); invalidation racing template teardown; corrupted styledChildren state after the tree was modified externally.","commonSituations":"Removing named children from a template at runtime while triggers still reference them; applying styles before the template has instantiated children; race between dispatcher operations that invalidate properties and template re-application.","solutions":["Verify the named child still exists in the template for every Trigger/Setters TargetName","Ensure the template has been applied (element is loaded) before invalidating child properties","Do not mutate template children from code; change templates/styles instead","Null-check before invalidation in custom code that walks template children"],"exampleFix":"// before\nchild = StyleHelper.GetChild(styledChildren, childIndex);\nStyleHelper.InvalidatePropertyOnTemplateFetch(...); // may throw if child removed\n// after\nchild = StyleHelper.GetChild(styledChildren, childIndex);\nif (child != null) { /* proceed with invalidation */ } else { return; }","handlingStrategy":"try-catch","validationCode":"bool ChildInstanceExists(FrameworkElement parent, int childIndex)\n{\n    var child = VisualTreeHelper.GetChild(parent, childIndex);\n    return child != null;\n}","typeGuard":null,"tryCatchPattern":"try { InvalidateChildProperty(childIndex); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"instance does not exist\"))\n{ ReapplyTemplate(); /* template children were torn down */ }","preventionTips":["Do not remove named children from applied templates at runtime","Apply/await template instantiation before invalidating child properties","Re-check child presence after template re-application"],"tags":["wpf","templates","state"],"backgroundTag":"entity-not-found","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"}