AvaloniaUI/Avalonia · error · InvalidOperationException
Invalid selector: Template selector must be followed by cont
Error message
Invalid selector: Template selector must be followed by control selector.
What it means
Thrown by Style.ValidateSelector (invoked from the Selector setter) when the assigned selector is a bare TemplateSelector with nothing following it. The Template selector (`/template/`) only makes sense when followed by a selector addressing a part inside the template; a lone TemplateSelector has no target part and is rejected at assignment time.
Source
Thrown at src/Avalonia.Base/Styling/Style.cs:96
SelectorMatch.NeverThisInstance);
activity?.AddTag(Diagnostic.Tags.SelectorResult, match.Result);
if (match.IsMatch)
{
Attach(target, match.Activator, type, Selector is not OrSelector);
}
result = match.Result;
}
return result;
}
private static Selector? ValidateSelector(Selector? selector)
{
if (selector is TemplateSelector)
throw new InvalidOperationException(
"Invalid selector: Template selector must be followed by control selector.");
return selector;
}
}
}
View on GitHub (pinned to 11c5427268)
Solutions
- Follow the Template selector with a part selector, e.g. `s.OfType<Button>().Template().OfType<ContentPresenter>()`.
- In AXAML, write `Button /template/ ContentPresenter` rather than just `Button /template/`.
- If you did not intend template traversal, remove the Template selector.
Example fix
// before new Style(s => s.OfType<Button>().Template()) // after new Style(s => s.OfType<Button>().Template().OfType<ContentPresenter>())
Defensive patterns
Strategy: validation
Validate before calling
// Reject a bare TemplateSelector before assigning to Style.Selector.
if (selector is TemplateSelector)
throw new InvalidOperationException("Template selector must be followed by a part selector."); Type guard
static bool IsCompleteTemplateChain(Selector? s) => s is not TemplateSelector;
Prevention
- Always follow Template() with a part selector (OfType/Class/etc.).
- In AXAML, write `Control /template/ Part` not just `Control /template/`.
- Build a helper that asserts the chain is non-trivial after a template step.
When it happens
Trigger: Assigning `s.Template()` alone as a Style's Selector (no subsequent OfType/Class/etc.), or AXAML `Selector="... /template/"` with nothing after the template token.
Common situations: Truncated selector after refactoring; AXAML where the post-template part was deleted; programmatic selector building that stops at Template().
Related errors
- ControlTemplate styles cannot contain multiple template sele
- Need more than one query to OR.
- Need more than one selector to OR.
- ControlTheme style may not directly contain a child or desce
- Child styles must have a nesting selector.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/31c919274bc470c7.
Report an issue: GitHub.