{"record":{"id":"f607bdfe141486f1","repo":"AvaloniaUI/Avalonia","slug":"cannot-find-a-datacontext-to-bind-to","errorCode":null,"errorMessage":"Cannot find a DataContext to bind to.","messagePattern":"Cannot find a DataContext to bind to\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/DataContextNodeBase.cs","lineNumber":17,"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 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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before - applied before element is attached / on non-provider\ncontrol.Bind(TextBlock.TextProperty, new Binding { /* DataContext-rooted */ });\n// after\ncontrol.DataContext = viewModel; // ensure provider + data context present first","handlingStrategy":"validation","validationCode":"// Ensure a DataContext provider exists before applying a DataContext-rooted binding.\nif (target is not IDataContextProvider || target is not AvaloniaObject)\n    throw new InvalidOperationException(\"Target cannot provide a DataContext.\");\nif (((IDataContextProvider)target).DataContext is null)\n    /* set DataContext or defer the binding until attached */;","typeGuard":"static bool HasDataContext(object o) => o is IDataContextProvider and AvaloniaObject;","tryCatchPattern":"try { target.Bind(prop, binding); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DataContext\"))\n{ /* defer until the element is attached to the logical tree */ }","preventionTips":["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."],"tags":["data-binding","datacontext","logical-tree"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}