{"record":{"id":"02b0fdc1f47c69cc","repo":"lepoco/wpfui","slug":"template-part-name-is-not-found-or-is-not-of-t","errorCode":null,"errorMessage":"Template part '{name}' is not found or is not of type {typeof(T)}","messagePattern":"Template part '(.+?)' is not found or is not of type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/TitleBar/TitleBar.cs","lineNumber":778,"sourceCode":"        if (dpiScale is null)\n        {\n            throw new InvalidOperationException(\"dpiScale is not initialized.\");\n        }\n\n        SystemCommands.ShowSystemMenu(\n            _parentWindow as Window,\n            new Point(point.X / dpiScale.Value.DpiScaleX, point.Y / dpiScale.Value.DpiScaleY)\n        );\n    }\n\n    private T GetTemplateChild<T>(string name)\n        where T : DependencyObject\n    {\n        DependencyObject element = GetTemplateChild(name);\n\n        if (element is not T tElement)\n        {\n            throw new InvalidOperationException(\n                $\"Template part '{name}' is not found or is not of type {typeof(T)}\"\n            );\n        }\n\n        return tElement;\n    }\n}\n","sourceCodeStart":760,"sourceCodeEnd":786,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs#L760-L786","documentation":"TitleBar.GetTemplateChild<T> is a strict helper used internally to fetch named template parts. It calls the base GetTemplateChild and throws InvalidOperationException if the element is missing or not of the requested type T. This is the generalised version of the SplitButton contract check and fires during OnApplyTemplate/initialisation whenever a required part is absent or mistyped in a custom ControlTemplate.","triggerScenarios":"A custom TitleBar ControlTemplate is missing one of the required named parts (e.g. the caption buttons, the close/minimize buttons, or the title content host), or names a part with the right name but the wrong element type.","commonSituations":"Re-templating TitleBar and dropping a template part; targeting an older/newer WPF UI version whose required part set changed; renaming parts in a fork.","solutions":["Carry over all required template parts from the default TitleBar template (look for x:Name=\"PART_*\").","Match the exact element type each part expects.","Remove your custom ControlTemplate if you do not need to restyle, letting the default template apply.","Diff your template against the shipping one after upgrading WPF UI."],"exampleFix":"<!-- ensure all PART_ elements exist with correct types -->\n<ControlTemplate TargetType=\"{x:Type ui:TitleBar}\">\n    <Grid>\n        <ContentPresenter x:Name=\"PART_TitleHost\" />\n        <StackPanel x:Name=\"PART_RightActionButtons\" Orientation=\"Horizontal\">\n            <ui:TitleBarButton x:Name=\"PART_MinimizeButton\" ButtonType=\"Minimize\" />\n            <ui:TitleBarButton x:Name=\"PART_CloseButton\" ButtonType=\"Close\" />\n        </StackPanel>\n    </Grid>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"// Confirm all PART_ names exist with correct types after template load\nforeach (var (name, type) in requiredParts)\n{\n    var el = template.LoadContent();\n    var found = el.GetType().GetField(name) ?? (el as FrameworkElement)?.FindName(name);\n    if (found?.GetType().IsAssignableTo(type) is not true)\n        throw new InvalidOperationException($\"Missing/mistyped part {name}\");\n}","typeGuard":"bool HasAllParts(ControlTemplate tpl, IEnumerable<(string Name, Type T)> parts)\n{\n    var root = tpl.LoadContent() as FrameworkElement;\n    return parts.All(p => root?.FindName(p.Name) is var e && e is not null && p.T.IsAssignableFrom(e.GetType()));\n}","tryCatchPattern":"try { titleBar.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Template part\"))\n{\n    _logger.LogError(ex, \"Custom TitleBar template is missing a required part.\");\n}","preventionTips":["Always carry required PART_ elements when authoring custom TitleBar templates.","Diff custom templates against the default after each WPF UI upgrade.","Use partial restyling (Triggers, Resources) when a full template is unnecessary."],"tags":["template-parts","titlebar","control-template","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}