AvaloniaUI/Avalonia · error · NotSupportedException
DataContextNode is invalid in conjunction with a binding sou
Error message
DataContextNode is invalid in conjunction with a binding source.
What it means
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.
Source
Thrown at src/Avalonia.Base/Data/Core/ExpressionNodes/DataContextNodeBase.cs:11
using System;
using Avalonia.LogicalTree;
namespace Avalonia.Data.Core.ExpressionNodes;
internal abstract class DataContextNodeBase : SourceNode
{
public override object? SelectSource(object? source, object target, object? anchor)
{
if (source != AvaloniaProperty.UnsetValue)
throw new NotSupportedException(
"DataContextNode is invalid in conjunction with a binding source.");
if (target is IDataContextProvider and AvaloniaObject)
return target;
if (anchor is IDataContextProvider and AvaloniaObject)
return anchor;
throw new InvalidOperationException("Cannot find a DataContext to bind to.");
}
}
View on GitHub (pinned to 11c5427268)
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).
Example fix
// before
{Binding Source={StaticResource vm}, Name}
<!-- Name is resolved against DataContext AND a Source is set -->
// after
{Binding Source={StaticResource vm}}
<!-- or -->
{Binding Name} Defensive patterns
Strategy: validation
Validate before calling
// A DataContext-rooted path and an explicit Source are mutually exclusive.
bool HasExplicitSource(Binding b) =>
b.Source is not null || b.RelativeSource is not null || !string.IsNullOrEmpty(b.ElementName);
if (IsDataContextRootedPath(path) && HasExplicitSource(binding))
throw new InvalidOperationException("Cannot set Source on a DataContext-rooted binding."); Try / catch
try { target.Bind(prop, binding); }
catch (NotSupportedException ex) when (ex.Message.Contains("DataContextNode"))
{ /* remove Source or change the path root */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot find a DataContext to bind to.
- LogicalAncestorNode is invalid in conjunction with a binding
- Cannot find an ILogical to get a logical ancestor.
- TemplatedParentNode is invalid in conjunction with a binding
- Cannot find a StyledElement to get a TemplatedParent.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/086e2005346411bc.
Report an issue: GitHub.