{"record":{"id":"211a33e415698eb0","repo":"dotnet/maui","slug":"binding-and-value-found-for-propertyname","errorCode":null,"errorMessage":"Binding and Value found for {PropertyName}","messagePattern":"Binding and Value found for (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/DataTemplate.cs","lineNumber":92,"sourceCode":"\t\tinternal override void SetupContent(object item)\n\t\t{\n\t\t\tApplyBindings(item);\n\t\t\tApplyValues(item);\n\t\t}\n\n\t\tvoid ApplyBindings(object item)\n\t\t{\n\t\t\tif (Bindings == null)\n\t\t\t\treturn;\n\n\t\t\tvar bindable = item as BindableObject;\n\t\t\tif (bindable == null)\n\t\t\t\treturn;\n\n\t\t\tforeach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)\n\t\t\t{\n\t\t\t\tif (Values.ContainsKey(kvp.Key))\n\t\t\t\t\tthrow new InvalidOperationException(\"Binding and Value found for \" + kvp.Key.PropertyName);\n\n\t\t\t\tbindable.SetBinding(kvp.Key, kvp.Value.Clone());\n\t\t\t}\n\t\t}\n\n\t\tvoid ApplyValues(object item)\n\t\t{\n\t\t\tif (Values == null)\n\t\t\t\treturn;\n\n\t\t\tvar bindable = item as BindableObject;\n\t\t\tif (bindable == null)\n\t\t\t\treturn;\n\t\t\tforeach (KeyValuePair<BindableProperty, object> kvp in Values)\n\t\t\t\tbindable.SetValue(kvp.Key, kvp.Value);\n\t\t}\n\t}\n}","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/DataTemplate.cs#L74-L110","documentation":"A DataTemplate throws InvalidOperationException when the same BindableProperty is present in BOTH its Bindings dictionary and its Values dictionary at the moment SetupContent applies bindings. The framework cannot apply a dynamic Binding and a static Value to the same property simultaneously, since they would conflict. The public SetBinding/SetValue methods already cross-remove the key from the other dictionary, so this throw is reachable mainly when a developer manipulates the public Bindings/Values dictionaries directly.","triggerScenarios":"Directly adding the same BindableProperty key to both DataTemplate.Bindings and DataTemplate.Values; or code that adds to Bindings/Values without going through SetBinding/SetValue; or two separate code paths (e.g. XAML compilation + runtime code) that each set a different mode on the same property after the dictionaries were touched by hand.","commonSituations":"Programmatic DataTemplate construction where a developer writes template.Values[prop] = x and template.Bindings[prop] = binding; XAML that sets an explicit attribute value and a Binding on the same property inside a DataTemplate; refactoring that switches a property from a value to a binding but leaves a stale dictionary entry.","solutions":["Use DataTemplate.SetBinding() and DataTemplate.SetValue() instead of touching the Bindings/Values dictionaries directly — these methods remove the conflicting key from the other dictionary.","Search your code for direct dictionary access (template.Bindings[...] = / template.Values[...] =) and replace with SetBinding/SetValue.","If you must manipulate the dictionaries, remove the property from the other dictionary before adding: ensure a key exists in only one of the two."],"exampleFix":"// before\ntemplate.Values[Label.TextProperty] = \"Hi\";\ntemplate.Bindings[Label.TextProperty] = new Binding(\"Name\"); // later code path, key now in both\n\n// after\ntemplate.SetValue(Label.TextProperty, \"Hi\");\ntemplate.SetBinding(Label.TextProperty, new Binding(\"Name\")); // SetValue removed first, SetBinding replaces","handlingStrategy":"validation","validationCode":"// Before relying on a DataTemplate's dictionaries, assert no key overlaps.\nbool HasConflict(DataTemplate t) =>\n    t.Bindings.Keys.Any(k => t.Values.ContainsKey(k));\n\nif (HasConflict(template))\n    throw new InvalidOperationException(\"DataTemplate has both Binding and Value for the same property.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never index into DataTemplate.Bindings / DataTemplate.Values directly — use SetBinding/SetValue.","In code review, flag any template.Bindings[...] = or template.Values[...] = assignment.","Keep each property's mode (binding vs value) decided at a single initialization site."],"tags":["datatemplate","binding","xaml","invalidoperation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}