dotnet/maui · error · InvalidOperationException

Binding and Value found for {PropertyName}

Error message

Binding and Value found for {PropertyName}

What it means

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.

Source

Thrown at src/Controls/src/Core/DataTemplate.cs:92

		internal override void SetupContent(object item)
		{
			ApplyBindings(item);
			ApplyValues(item);
		}

		void ApplyBindings(object item)
		{
			if (Bindings == null)
				return;

			var bindable = item as BindableObject;
			if (bindable == null)
				return;

			foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
			{
				if (Values.ContainsKey(kvp.Key))
					throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);

				bindable.SetBinding(kvp.Key, kvp.Value.Clone());
			}
		}

		void ApplyValues(object item)
		{
			if (Values == null)
				return;

			var bindable = item as BindableObject;
			if (bindable == null)
				return;
			foreach (KeyValuePair<BindableProperty, object> kvp in Values)
				bindable.SetValue(kvp.Key, kvp.Value);
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use DataTemplate.SetBinding() and DataTemplate.SetValue() instead of touching the Bindings/Values dictionaries directly — these methods remove the conflicting key from the other dictionary.
  2. Search your code for direct dictionary access (template.Bindings[...] = / template.Values[...] =) and replace with SetBinding/SetValue.
  3. 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.

Example fix

// before
template.Values[Label.TextProperty] = "Hi";
template.Bindings[Label.TextProperty] = new Binding("Name"); // later code path, key now in both

// after
template.SetValue(Label.TextProperty, "Hi");
template.SetBinding(Label.TextProperty, new Binding("Name")); // SetValue removed first, SetBinding replaces
Defensive patterns

Strategy: validation

Validate before calling

// Before relying on a DataTemplate's dictionaries, assert no key overlaps.
bool HasConflict(DataTemplate t) =>
    t.Bindings.Keys.Any(k => t.Values.ContainsKey(k));

if (HasConflict(template))
    throw new InvalidOperationException("DataTemplate has both Binding and Value for the same property.");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/211a33e415698eb0. Report an issue: GitHub.