{"record":{"id":"6bd3b35382af1849","repo":"AvaloniaUI/Avalonia","slug":"logicalancestornode-is-invalid-in-conjunction-with","errorCode":null,"errorMessage":"LogicalAncestorNode is invalid in conjunction with a binding source.","messagePattern":"LogicalAncestorNode is invalid in conjunction with a binding source\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/LogicalAncestorElementNode.cs","lineNumber":45,"sourceCode":"\n            if (_ancestorType is not null)\n            {\n                builder.Append(_ancestorType.Name);\n                if (_ancestorLevel > 0)\n                    builder.Append(',');\n            }\n\n            if (_ancestorLevel > 0)\n                builder.Append(_ancestorLevel);\n\n            builder.Append(']');\n        }\n    }\n\n    public override object? SelectSource(object? source, object target, object? anchor)\n    {\n        if (source != AvaloniaProperty.UnsetValue)\n            throw new NotSupportedException(\n                \"LogicalAncestorNode is invalid in conjunction with a binding source.\");\n        if (target is ILogical)\n            return target;\n        if (anchor is ILogical)\n            return anchor;\n        throw new InvalidOperationException(\"Cannot find an ILogical to get a logical ancestor.\");\n    }\n\n    public override bool ShouldLogErrors(object target)\n    {\n        return target is ILogical logical && logical.IsAttachedToLogicalTree;\n    }\n\n    protected override void OnSourceChanged(object? source, Exception? dataValidationError)\n    {\n        if (!ValidateNonNullSource(source))\n            return;\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/ExpressionNodes/LogicalAncestorElementNode.cs#L27-L63","documentation":"LogicalAncestorElementNode.SelectSource throws NotSupportedException when an explicit binding source is set (source != UnsetValue) together with a $parent logical-ancestor relative source. The logical-ancestor traversal walks the logical tree from the target, so an explicit Source is contradictory and not allowed.","triggerScenarios":"Combining Binding.Source with a path that uses a logical-ancestor RelativeSource ($parent[...] or $self-relative), or specifying both a Source and RelativeSource in the same binding.","commonSituations":"Adding Source= to a control-template binding that already uses $parent; mixing ElementName/Source with RelativeSource in one binding descriptor.","solutions":["Remove the explicit Source so the logical-ancestor RelativeSource can traverse from the target.","If you need a fixed source, drop the logical-ancestor relative source and bind the source object directly."],"exampleFix":"// before\n{Binding $parent[ItemsControl].Items, Source={StaticResource x}}\n// after\n{Binding $parent[ItemsControl].Items}","handlingStrategy":"validation","validationCode":"// Logical-ancestor RelativeSource and explicit Source are mutually exclusive.\nbool UsesLogicalAncestor(Binding b) =>\n    b.RelativeSource is RelativeSource rs && rs.Mode == RelativeSourceMode.FindAncestor;\n\nif (UsesLogicalAncestor(binding) && binding.Source is not null)\n    throw new InvalidOperationException(\"Cannot combine logical-ancestor RelativeSource with Source.\");","typeGuard":null,"tryCatchPattern":"try { target.Bind(prop, binding); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"LogicalAncestorNode\"))\n{ /* drop Source or the relative source */ }","preventionTips":["Use exactly one source strategy per binding.","In control templates, prefer $parent/$self over mixing with Source.","Review bindings after refactoring RelativeSource usage."],"tags":["data-binding","relative-source","logical-tree","ancestor"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}