{"record":{"id":"5132edaa4dc1ce49","repo":"AvaloniaUI/Avalonia","slug":"cannot-create-elementname-binding-when-namescope-i","errorCode":null,"errorMessage":"Cannot create ElementName binding when NameScope is null","messagePattern":"Cannot create ElementName binding when NameScope is null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/ReflectionBinding.cs","lineNumber":198,"sourceCode":"                stringFormat: StringFormat,\n                targetProperty: targetProperty,\n                targetNullValue: TargetNullValue,\n                targetTypeConverter: TargetTypeConverter.GetReflectionConverter(),\n                updateSourceTrigger: trigger);\n        }\n\n        private INameScope? GetNameScope()\n        {\n            INameScope? result = null;\n            NameScope?.TryGetTarget(out result);\n            return result;\n        }\n\n        private ExpressionNode? CreateSourceNode(AvaloniaProperty? targetProperty)\n        {\n            if (!string.IsNullOrEmpty(ElementName))\n            {\n                var nameScope = GetNameScope() ?? throw new InvalidOperationException(\n                    \"Cannot create ElementName binding when NameScope is null\");\n                return new NamedElementNode(nameScope, ElementName);\n            }\n\n            if (RelativeSource is not null)\n                return ExpressionNodeFactory.CreateRelativeSource(RelativeSource);\n\n            return ExpressionNodeFactory.CreateDataContext(targetProperty);\n        }\n\n        private (BindingMode, UpdateSourceTrigger) ResolveDefaultsFromMetadata(\n            AvaloniaObject target,\n            AvaloniaProperty? targetProperty)\n        {\n            var mode = Mode;\n            var trigger = UpdateSourceTrigger == UpdateSourceTrigger.Default ?\n                UpdateSourceTrigger.PropertyChanged : UpdateSourceTrigger;\n","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/ReflectionBinding.cs#L180-L216","documentation":"ElementName bindings resolve the named element through the current INameScope (the XAML namescope). ReflectionBinding.CreateSourceNode throws when ElementName is set but no NameScope is available, because there is no registry to look the name up in. This typically means the binding was created outside a XAML/visual-tree context that owns a namescope.","triggerScenarios":"Creating a ReflectionBinding with ElementName set and applying it where no INameScope is registered (e.g. on a detached object, or constructed in code without a namescope). Also when NameScope is a WeakReference whose target has been collected.","commonSituations":"Setting an ElementName binding in code-behind on an element not yet in the visual/logical tree. Binding before the template's namescope is applied. Weak reference to the namescope being garbage-collected.","solutions":["Apply the binding after the element is in the visual tree so a namescope is available.","Ensure the XAML defining the named element is loaded (namescope registered) before the binding resolves.","Prefer CompiledBindings over ReflectionBinding so ElementName resolution uses the compiled namescope graph.","If building in code, supply/attach an INameScope (NameScope.SetNameScope) containing the named element."],"exampleFix":"// before (binding before namescope ready):\nvar b = new ReflectionBinding(\"ElementName=MyOther\") { /*...*/ };\ndetachedControl.Bind(SomeControl.SomeProperty, b); // NameScope null -> throws\n\n// after (apply once in tree):\nthis.AttachedToVisualTree += (s, e) =>\n    this.Bind(SomeControl.SomeProperty, b);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(binding.ElementName) is false && GetNameScope() is null)\n    throw new InvalidOperationException(\"ElementName binding needs a namescope.\");","typeGuard":"static bool HasNameScope(INameScope? ns) => ns is not null;","tryCatchPattern":null,"preventionTips":["Apply ElementName bindings after the element is in the visual tree.","Prefer CompiledBindings for ElementName resolution.","If constructing in code, attach an INameScope with NameScope.SetNameScope."],"tags":["bindings","elementname","namescope","xaml","invalidoperation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}