lepoco/wpfui · error · InvalidOperationException

Template part '{name}' is not found or is not of type {typeo

Error message

Template part '{name}' is not found or is not of type {typeof(T)}

What it means

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.

Source

Thrown at src/Wpf.Ui/Controls/TitleBar/TitleBar.cs:778

        if (dpiScale is null)
        {
            throw new InvalidOperationException("dpiScale is not initialized.");
        }

        SystemCommands.ShowSystemMenu(
            _parentWindow as Window,
            new Point(point.X / dpiScale.Value.DpiScaleX, point.Y / dpiScale.Value.DpiScaleY)
        );
    }

    private T GetTemplateChild<T>(string name)
        where T : DependencyObject
    {
        DependencyObject element = GetTemplateChild(name);

        if (element is not T tElement)
        {
            throw new InvalidOperationException(
                $"Template part '{name}' is not found or is not of type {typeof(T)}"
            );
        }

        return tElement;
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Carry over all required template parts from the default TitleBar template (look for x:Name="PART_*").
  2. Match the exact element type each part expects.
  3. Remove your custom ControlTemplate if you do not need to restyle, letting the default template apply.
  4. Diff your template against the shipping one after upgrading WPF UI.

Example fix

<!-- ensure all PART_ elements exist with correct types -->
<ControlTemplate TargetType="{x:Type ui:TitleBar}">
    <Grid>
        <ContentPresenter x:Name="PART_TitleHost" />
        <StackPanel x:Name="PART_RightActionButtons" Orientation="Horizontal">
            <ui:TitleBarButton x:Name="PART_MinimizeButton" ButtonType="Minimize" />
            <ui:TitleBarButton x:Name="PART_CloseButton" ButtonType="Close" />
        </StackPanel>
    </Grid>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// Confirm all PART_ names exist with correct types after template load
foreach (var (name, type) in requiredParts)
{
    var el = template.LoadContent();
    var found = el.GetType().GetField(name) ?? (el as FrameworkElement)?.FindName(name);
    if (found?.GetType().IsAssignableTo(type) is not true)
        throw new InvalidOperationException($"Missing/mistyped part {name}");
}

Type guard

bool HasAllParts(ControlTemplate tpl, IEnumerable<(string Name, Type T)> parts)
{
    var root = tpl.LoadContent() as FrameworkElement;
    return parts.All(p => root?.FindName(p.Name) is var e && e is not null && p.T.IsAssignableFrom(e.GetType()));
}

Try / catch

try { titleBar.ApplyTemplate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Template part"))
{
    _logger.LogError(ex, "Custom TitleBar template is missing a required part.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/02b0fdc1f47c69cc. Report an issue: GitHub.