AvaloniaUI/Avalonia · error · InvalidOperationException
Cannot find a StyledElement to get a TemplatedParent.
Error message
Cannot find a StyledElement to get a TemplatedParent.
What it means
TemplatedParentNode.SelectSource throws InvalidOperationException when neither target nor anchor is a StyledElement. TemplatedParent is a StyledElement property, so a non-StyledElement target cannot supply it.
Source
Thrown at src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs:22
namespace Avalonia.Data.Core.ExpressionNodes;
internal sealed class TemplatedParentNode : SourceNode
{
public override void BuildString(StringBuilder builder)
{
builder.Append("$templatedParent");
}
public override object? SelectSource(object? source, object target, object? anchor)
{
if (source != AvaloniaProperty.UnsetValue)
throw new NotSupportedException(
"TemplatedParentNode is invalid in conjunction with a binding source.");
if (target is StyledElement)
return target;
if (anchor is StyledElement)
return anchor;
throw new InvalidOperationException("Cannot find a StyledElement to get a TemplatedParent.");
}
protected override void OnSourceChanged(object? source, Exception? dataValidationError)
{
if (!ValidateNonNullSource(source))
return;
if (source is StyledElement newElement)
{
newElement.PropertyChanged += OnPropertyChanged;
SetValue(newElement.TemplatedParent);
}
else
{
SetError($"Unable to read TemplatedParent from '{source.GetType()}'.");
}
}
View on GitHub (pinned to 11c5427268)
Solutions
- Use $templatedParent only inside a ControlTemplate applied to a StyledElement.
- Outside templates, bind to the real source object or use a different RelativeSource ($self, $parent).
Example fix
// before - outside a template
<TextBlock Text="{Binding $templatedParent.Title}" />
// after - inside the ControlTemplate of a templated control
<ControlTemplate><TextBlock Text="{Binding $templatedParent.Title}" /></ControlTemplate> Defensive patterns
Strategy: validation
Validate before calling
// TemplatedParent requires a StyledElement target.
if (target is not StyledElement)
throw new InvalidOperationException("Target is not a StyledElement; cannot resolve TemplatedParent."); Type guard
static bool CanResolveTemplatedParent(object o) => o is StyledElement;
Try / catch
try { target.Bind(prop, binding); }
catch (InvalidOperationException ex) when (ex.Message.Contains("StyledElement"))
{ /* use a non-template binding or a different relative source */ } Prevention
- Use $templatedParent only inside ControlTemplates.
- Outside templates, bind the real source or use $self/$parent.
- Confirm the target is a StyledElement before templated-parent bindings.
When it happens
Trigger: Using a $templatedParent binding outside a ControlTemplate (no templated parent exists), or on an object that is not a StyledElement.
Common situations: Using $templatedParent in a normal (non-template) binding; applying a templated-parent binding to a raw Visual or non-styled element; a control that has no template applied.
Related errors
- TemplatedParentNode is invalid in conjunction with a binding
- DataContextNode is invalid in conjunction with a binding sou
- LogicalAncestorNode is invalid in conjunction with a binding
- Cannot find an ILogical to get a logical ancestor.
- Cannot find an ILogical to get a visual ancestor.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/2128c8e524dd6c21.
Report an issue: GitHub.