{"record":{"id":"086e2005346411bc","repo":"AvaloniaUI/Avalonia","slug":"datacontextnode-is-invalid-in-conjunction-with-a-b","errorCode":null,"errorMessage":"DataContextNode is invalid in conjunction with a binding source.","messagePattern":"DataContextNode is invalid in conjunction with a binding source\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/DataContextNodeBase.cs","lineNumber":11,"sourceCode":"﻿using System;\nusing Avalonia.LogicalTree;\n\nnamespace Avalonia.Data.Core.ExpressionNodes;\n\ninternal abstract class DataContextNodeBase : SourceNode\n{\n    public override object? SelectSource(object? source, object target, object? anchor)\n    {\n        if (source != AvaloniaProperty.UnsetValue)\n            throw new NotSupportedException(\n                \"DataContextNode is invalid in conjunction with a binding source.\");\n        if (target is IDataContextProvider and AvaloniaObject)\n            return target;\n        if (anchor is IDataContextProvider and AvaloniaObject)\n            return anchor;\n        throw new InvalidOperationException(\"Cannot find a DataContext to bind to.\");\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/ExpressionNodes/DataContextNodeBase.cs#L1-L20","documentation":"DataContextNodeBase.SelectSource throws when a binding source was explicitly provided (source != AvaloniaProperty.UnsetValue) AND the path resolves through $DataContext (or the implicit data-context root). The two are mutually exclusive: a DataContext-relative path reads the inherited DataContext, so an explicit Source contradicts it. The library fails fast instead of silently preferring one.","triggerScenarios":"Setting both Binding.Source (or RelativeSource/ElementName) and a path that begins from the data context. Equivalent to mixing a DataContext-rooted compiled binding with an explicit source object.","commonSituations":"Refactoring a {Binding} to add Source= while leaving the path implicitly DataContext-rooted; combining RelativeSource with a path meant for DataContext; copy-paste from a DataContext binding into a control-template binding without removing the data-context root.","solutions":["Remove the explicit Source so the path resolves through DataContext as intended.","If you need an explicit source, change the path so it does not start from DataContext (e.g. use $self or a direct property path on the source object)."],"exampleFix":"// before\n{Binding Source={StaticResource vm}, Name}\n<!-- Name is resolved against DataContext AND a Source is set -->\n// after\n{Binding Source={StaticResource vm}}\n<!-- or -->\n{Binding Name}","handlingStrategy":"validation","validationCode":"// A DataContext-rooted path and an explicit Source are mutually exclusive.\nbool HasExplicitSource(Binding b) =>\n    b.Source is not null || b.RelativeSource is not null || !string.IsNullOrEmpty(b.ElementName);\n\nif (IsDataContextRootedPath(path) && HasExplicitSource(binding))\n    throw new InvalidOperationException(\"Cannot set Source on a DataContext-rooted binding.\");","typeGuard":null,"tryCatchPattern":"try { target.Bind(prop, binding); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"DataContextNode\"))\n{ /* remove Source or change the path root */ }","preventionTips":["Pick exactly one source strategy per binding: DataContext, Source, RelativeSource, or ElementName.","When copying a binding, clear unrelated source fields.","Audit control-template bindings that add Source alongside inherited paths."],"tags":["data-binding","datacontext","source","relative-source"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}