{"record":{"id":"2670f609516ae3dd","repo":"lepoco/wpfui","slug":"element-nameof-templateelementtogglebutton-of-t","errorCode":null,"errorMessage":"Element {nameof(TemplateElementToggleButton)} of type {typeof(ToggleButton)} not found in {typeof(SplitButton)}","messagePattern":"Element (.+?) of type (.+?) not found in (.+?)","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/SplitButton/SplitButton.cs","lineNumber":159,"sourceCode":"    }\n\n    /// <summary>This method is invoked when the <see cref=\"IsDropDownOpenProperty\"/> changes.</summary>\n    /// <param name=\"currentValue\">The new value of <see cref=\"IsDropDownOpenProperty\"/>.</param>\n    protected virtual void OnIsDropDownOpenChanged(bool currentValue) { }\n\n    /// <inheritdoc />\n    public override void OnApplyTemplate()\n    {\n        base.OnApplyTemplate();\n\n        if (GetTemplateChild(TemplateElementToggleButton) is ToggleButton toggleButton)\n        {\n            SplitButtonToggleButton = toggleButton;\n            AttachToggleButtonClick();\n        }\n        else\n        {\n            throw new NullReferenceException(\n                $\"Element {nameof(TemplateElementToggleButton)} of type {typeof(ToggleButton)} not found in {typeof(SplitButton)}\"\n            );\n        }\n\n        if (GetTemplateChild(TemplateElementToggle) is Border toggleBorder)\n        {\n            _splitButtonToggleBorder = toggleBorder;\n        }\n    }\n\n    /// <summary>\n    /// Triggered when the control is unloaded. Releases resource bindings.\n    /// </summary>\n    protected virtual void ReleaseTemplateResources()\n    {\n        if (SplitButtonToggleButton != null)\n        {\n            SplitButtonToggleButton.PreviewMouseLeftButtonUp -=","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/SplitButton/SplitButton.cs#L141-L177","documentation":"During OnApplyTemplate, SplitButton looks up the named template part 'PART_SplitButtonToggleButton' and requires it to be a ToggleButton. If GetTemplateChild returns null or a non-ToggleButton, a NullReferenceException is thrown. SplitButton cannot function without this part because it drives the drop-down toggle behaviour, so a custom ControlTemplate missing it is treated as a fatal template contract violation.","triggerScenarios":"Replacing the default SplitButton ControlTemplate with a custom one that omits the ToggleButton named PART_SplitButtonToggleButton, or names a control of a different type with that name.","commonSituations":"Restyling SplitButton and forgetting to carry over the required template part; renaming the part during a version upgrade; copying a template from a different control.","solutions":["Include <ToggleButton x:Name=\"PART_SplitButtonToggleButton\" /> in your custom ControlTemplate for SplitButton.","If you do not need a custom template, remove your Style/Template so the default one is used.","Update your template to match the template-part contract declared on SplitButton for the version you target."],"exampleFix":"<!-- before: custom template missing the toggle -->\n<ControlTemplate TargetType=\"{x:Type ui:SplitButton}\">\n    <Border x:Name=\"PrimaryBorder\" />\n</ControlTemplate>\n\n<!-- after -->\n<ControlTemplate TargetType=\"{x:Type ui:SplitButton}\">\n    <StackPanel Orientation=\"Horizontal\">\n        <ContentPresenter />\n        <ToggleButton x:Name=\"PART_SplitButtonToggleButton\" />\n    </StackPanel>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"// Validate template before applying in design/test\nvar hasPart = myTemplate.LoadContent() is FrameworkElement fe\n    && fe.FindName(\"PART_SplitButtonToggleButton\") is ToggleButton;\nif (!hasPart) throw new InvalidOperationException(\"Template missing PART_SplitButtonToggleButton\");","typeGuard":"bool TemplateHasRequiredPart(ControlTemplate tpl, Type owner) =>\n    tpl.LoadContent() is FrameworkElement root && root.FindName(\"PART_SplitButtonToggleButton\") is ToggleButton;","tryCatchPattern":"try { ApplyTemplate(); }\ncatch (NullReferenceException ex) when (ex.Message.Contains(\"PART_SplitButtonToggleButton\"))\n{\n    _logger.LogError(ex, \"Custom SplitButton template is missing the required toggle part.\");\n}","preventionTips":["Always copy the required PART_ elements when restyling templated controls.","After upgrading WPF UI, diff your custom templates against the new defaults.","Prefer Trigger-based restyling over full ControlTemplate replacement when possible."],"tags":["template-parts","splitbutton","control-template","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}