{"record":{"id":"a8f7678b817db356","repo":"AvaloniaUI/Avalonia","slug":"templatedparentnode-is-invalid-in-conjunction-with","errorCode":null,"errorMessage":"TemplatedParentNode is invalid in conjunction with a binding source.","messagePattern":"TemplatedParentNode is invalid in conjunction with a binding source\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs","lineNumber":16,"sourceCode":"﻿using System;\nusing System.Text;\n\nnamespace Avalonia.Data.Core.ExpressionNodes;\n\ninternal sealed class TemplatedParentNode : SourceNode\n{\n    public override void BuildString(StringBuilder builder)\n    {         \n        builder.Append(\"$templatedParent\");\n    }\n\n    public override object? SelectSource(object? source, object target, object? anchor)\n    {\n        if (source != AvaloniaProperty.UnsetValue)\n            throw new NotSupportedException(\n                \"TemplatedParentNode is invalid in conjunction with a binding source.\");\n        if (target is StyledElement)\n            return target;\n        if (anchor is StyledElement)\n            return anchor;\n        throw new InvalidOperationException(\"Cannot find a StyledElement to get a TemplatedParent.\");\n    }\n\n    protected override void OnSourceChanged(object? source, Exception? dataValidationError)\n    {\n        if (!ValidateNonNullSource(source))\n            return;\n\n        if (source is StyledElement newElement)\n        {\n            newElement.PropertyChanged += OnPropertyChanged;\n            SetValue(newElement.TemplatedParent);\n        }","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs#L1-L34","documentation":"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.","triggerScenarios":"Setting Binding.Source (or another source descriptor) on a binding whose path begins with $templatedParent, typically inside a ControlTemplate.","commonSituations":"Adding Source= to a template binding; mixing RelativeSource={RelativeSource TemplatedParent} with an explicit Source on the same binding.","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."],"exampleFix":"// before\n{Binding $templatedParent.Foreground, Source={StaticResource x}}\n// after\n{Binding $templatedParent.Foreground}","handlingStrategy":"validation","validationCode":"// TemplatedParent RelativeSource and explicit Source are mutually exclusive.\nbool UsesTemplatedParent(Binding b) =>\n    b.RelativeSource is RelativeSource rs && rs.Mode == RelativeSourceMode.TemplatedParent;\n\nif (UsesTemplatedParent(binding) && binding.Source is not null)\n    throw new InvalidOperationException(\"Cannot combine TemplatedParent with Source.\");","typeGuard":null,"tryCatchPattern":"try { target.Bind(prop, binding); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"TemplatedParentNode\"))\n{ /* drop Source for templated-parent bindings */ }","preventionTips":["Reserve $templatedParent for ControlTemplate bindings only.","Do not set Source on a templated-parent binding.","Use TemplateBinding or $templatedParent consistently within a template."],"tags":["data-binding","relative-source","templated-parent","control-template"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}