AvaloniaUI/Avalonia · error · NotSupportedException
TemplatedParentNode is invalid in conjunction with a binding
Error message
TemplatedParentNode is invalid in conjunction with a binding source.
What it means
TemplatedParentNode.SelectSource throws NotSupportedException when an explicit binding source is set together with a $templatedParent relative source. The templated parent is resolved from the target's StyledElement.TemplatedParent, so specifying an explicit Source is contradictory and disallowed.
Source
Thrown at src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs:16
using System;
using System.Text;
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);
}View on GitHub (pinned to 11c5427268)
Solutions
- Remove the explicit Source so the binding resolves TemplatedParent from the target StyledElement.
- If a fixed source is required, drop $templatedParent and bind the source object directly.
Example fix
// before
{Binding $templatedParent.Foreground, Source={StaticResource x}}
// after
{Binding $templatedParent.Foreground} Defensive patterns
Strategy: validation
Validate before calling
// TemplatedParent RelativeSource and explicit Source are mutually exclusive.
bool UsesTemplatedParent(Binding b) =>
b.RelativeSource is RelativeSource rs && rs.Mode == RelativeSourceMode.TemplatedParent;
if (UsesTemplatedParent(binding) && binding.Source is not null)
throw new InvalidOperationException("Cannot combine TemplatedParent with Source."); Try / catch
try { target.Bind(prop, binding); }
catch (NotSupportedException ex) when (ex.Message.Contains("TemplatedParentNode"))
{ /* drop Source for templated-parent bindings */ } Prevention
- Reserve $templatedParent for ControlTemplate bindings only.
- Do not set Source on a templated-parent binding.
- Use TemplateBinding or $templatedParent consistently within a template.
When it happens
Trigger: Setting Binding.Source (or another source descriptor) on a binding whose path begins with $templatedParent, typically inside a ControlTemplate.
Common situations: Adding Source= to a template binding; mixing RelativeSource={RelativeSource TemplatedParent} with an explicit Source on the same binding.
Related errors
- Cannot find a StyledElement to get a TemplatedParent.
- 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/a8f7678b817db356.
Report an issue: GitHub.