AvaloniaUI/Avalonia · error · InvalidOperationException
Cannot find a DataContext to bind to.
Error message
Cannot find a DataContext to bind to.
What it means
DataContextNodeBase.SelectSource throws InvalidOperationException when neither the target nor the anchor is an IDataContextProvider that is also an AvaloniaObject. Without such an object there is no DataContext property to read from, so the DataContext-rooted binding cannot resolve a source.
Source
Thrown at src/Avalonia.Base/Data/Core/ExpressionNodes/DataContextNodeBase.cs:17
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
- Ensure the binding target is an AvaloniaObject that implements IDataContextProvider (a normal control).
- Set DataContext on the target (or an ancestor) before the binding evaluates.
- Delay or move the binding so it is established once the element is attached to the logical tree.
Example fix
// before - applied before element is attached / on non-provider
control.Bind(TextBlock.TextProperty, new Binding { /* DataContext-rooted */ });
// after
control.DataContext = viewModel; // ensure provider + data context present first Defensive patterns
Strategy: validation
Validate before calling
// Ensure a DataContext provider exists before applying a DataContext-rooted binding.
if (target is not IDataContextProvider || target is not AvaloniaObject)
throw new InvalidOperationException("Target cannot provide a DataContext.");
if (((IDataContextProvider)target).DataContext is null)
/* set DataContext or defer the binding until attached */; Type guard
static bool HasDataContext(object o) => o is IDataContextProvider and AvaloniaObject;
Try / catch
try { target.Bind(prop, binding); }
catch (InvalidOperationException ex) when (ex.Message.Contains("DataContext"))
{ /* defer until the element is attached to the logical tree */ } Prevention
- Apply DataContext-rooted bindings only to controls in the logical tree.
- Set DataContext on an ancestor before bindings evaluate.
- Defer bindings to OnAttachedToLogicalTree when timing is uncertain.
When it happens
Trigger: A DataContext-rooted binding applied to an object that is not an AvaloniaObject/IDataContextProvider, or applied before the target is attached to the logical tree so no DataContext provider is available.
Common situations: Binding to DataContext on a raw non-visual object; applying a binding to a control that has not yet joined the logical tree (timing); binding on a node type that does not implement IDataContextProvider.
Related errors
- DataContextNode is invalid in conjunction with a binding sou
- LogicalAncestorNode is invalid in conjunction with a binding
- Cannot find an ILogical to get a logical ancestor.
- Two way bindings are not supported with a string format
- Binding mode cannot be Default.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/f607bdfe141486f1.
Report an issue: GitHub.