{"record":{"id":"2128c8e524dd6c21","repo":"AvaloniaUI/Avalonia","slug":"cannot-find-a-styledelement-to-get-a-templatedpare","errorCode":null,"errorMessage":"Cannot find a StyledElement to get a TemplatedParent.","messagePattern":"Cannot find a StyledElement to get a TemplatedParent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs","lineNumber":22,"sourceCode":"namespace 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        }\n        else\n        {\n            SetError($\"Unable to read TemplatedParent from '{source.GetType()}'.\");\n        }\n    }\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/ExpressionNodes/TemplatedParentNode.cs#L4-L40","documentation":"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.","triggerScenarios":"Using a $templatedParent binding outside a ControlTemplate (no templated parent exists), or on an object that is not a StyledElement.","commonSituations":"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.","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)."],"exampleFix":"// before - outside a template\n<TextBlock Text=\"{Binding $templatedParent.Title}\" />\n// after - inside the ControlTemplate of a templated control\n<ControlTemplate><TextBlock Text=\"{Binding $templatedParent.Title}\" /></ControlTemplate>","handlingStrategy":"validation","validationCode":"// TemplatedParent requires a StyledElement target.\nif (target is not StyledElement)\n    throw new InvalidOperationException(\"Target is not a StyledElement; cannot resolve TemplatedParent.\");","typeGuard":"static bool CanResolveTemplatedParent(object o) => o is StyledElement;","tryCatchPattern":"try { target.Bind(prop, binding); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"StyledElement\"))\n{ /* use a non-template binding or a different relative source */ }","preventionTips":["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."],"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"}