{"record":{"id":"a23075c54d433f0d","repo":"dotnet/maui","slug":"cannot-apply-relative-binding-to-bindobj-gettype","errorCode":null,"errorMessage":"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element.","messagePattern":"Cannot apply relative binding to (.+?) because it is not a superclass of Element\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Binding.cs","lineNumber":165,"sourceCode":"\t\t\tobject src = _source;\n\t\t\tvar isApplied = IsApplied;\n\n\t\t\tvar bindingContext = src ?? Context ?? context;\n\t\t\tbase.Apply(bindingContext, bindObj, targetProperty, fromBindingContextChanged, specificity);\n\n\t\t\tif (src != null && isApplied && fromBindingContextChanged)\n\t\t\t\treturn;\n\n\t\t\tif (Source is RelativeBindingSource relativeBindingSource)\n\t\t\t{\n\t\t\t\tvar relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;\n\t\t\t\tif (relativeSourceTarget is not Element)\n\t\t\t\t{\n\t\t\t\t\tvar message = bindObj is not null\n\t\t\t\t\t\t? $\"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element.\"\n\t\t\t\t\t\t: \"Cannot apply relative binding when the target object is null.\";\n\n\t\t\t\t\tthrow new InvalidOperationException(message);\n\t\t\t\t}\n\n\t\t\t\tApplyRelativeSourceBinding(relativeBindingSource, relativeSourceTarget, bindObj, targetProperty, specificity);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (_expression == null)\n\t\t\t\t\t_expression = new BindingExpression(this, SelfPath);\n\t\t\t\t_expression.Apply(bindingContext, bindObj, targetProperty, specificity);\n\t\t\t}\n\t\t}\n\n#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void\n\t\tasync void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)\n#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void\n\t\t{\n\t\t\ttry\n\t\t\t{","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Binding.cs#L147-L183","documentation":"InvalidOperationException from Binding.Apply when Source is a RelativeBindingSource but the target object (after RelativeSourceTargetOverride fallback) is not a Maui Element. Relative bindings walk the visual/element tree (self, parent, ancestor-by-type), so they require an Element target; applying one to a non-Element BindableObject (or a null override) is rejected. The message reports the actual type found.","triggerScenarios":"Calling SetBinding with a Binding whose Source is RelativeBindingSource.Self/Parent/FindAncestor on a BindableObject that is not an Element (e.g. a bare BindableObject, a Behavior, or a non-visual object); setting RelativeSourceTargetOverride to a non-Element.","commonSituations":"Trying relative source bindings on view-model-like BindableObjects; porting a relative binding that worked on a VisualElement to a non-visual object; testing bindings against mock BindableObjects.","solutions":["Apply relative bindings only on Element-derived targets (VisualElement, Page, View, etc.).","If you need a non-element source, use an explicit Source or x:Static instead of RelativeBindingSource.","If you set RelativeSourceTargetOverride, pass an Element."],"exampleFix":"// before (applied to a non-Element BindableObject)\nmyBindableObject.SetBinding(MyProp, new Binding(\"X\", source: RelativeBindingSource.TemplatedParent));\n\n// after (apply on an Element, or use an explicit source)\nmyVisualElement.SetBinding(MyProp, new Binding(\"X\", source: RelativeBindingSource.TemplatedParent));","handlingStrategy":"type-guard","validationCode":"if (bindObj is not Microsoft.Maui.Controls.Element)\n    throw new InvalidOperationException(\"Relative bindings require an Element target.\");\nbindObj.SetBinding(MyProp, new Binding(\"X\", source: RelativeBindingSource.Self));","typeGuard":"static bool CanUseRelativeBinding(Microsoft.Maui.Controls.BindableObject target) =>\n    target is Microsoft.Maui.Controls.Element;","tryCatchPattern":null,"preventionTips":["Only apply relative bindings on VisualElement/Page/View.","For non-element targets use an explicit Source."],"tags":["maui","data-binding","relative-binding","element-tree","invalidoperation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}