AvaloniaUI/Avalonia · error · NotSupportedException

TemplatedParentNode is invalid in conjunction with a…

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.

Solutions

  1. Remove the explicit Source so the binding resolves TemplatedParent from the target StyledElement.
  2. 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

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


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/a8f7678b817db356. Report an issue: GitHub.

Appendix: 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)