dotnet/maui · error · ArgumentException

mode is not a valid BindingMode

Error message

mode is not a valid BindingMode

What it means

Thrown by the Mode property setter on BindingBase when the supplied value is not one of the valid BindingMode enum members (Default, OneWay, OneWayToSource, TwoWay, OneTime). The setter explicitly rejects any other integer/enum value, including undefined enum casts.

Source

Thrown at src/Controls/src/Core/BindingBase.cs:41

		object _fallbackValue;
		WeakReference<Element> _relativeSourceTargetOverride;

		internal BindingBase()
		{
		}

		/// <summary>Gets or sets the mode for this binding.</summary>
		public BindingMode Mode
		{
			get { return _mode; }
			set
			{
				if (value != BindingMode.Default
					&& value != BindingMode.OneWay
					&& value != BindingMode.OneWayToSource
					&& value != BindingMode.TwoWay
					&& value != BindingMode.OneTime)
					throw new ArgumentException("mode is not a valid BindingMode", "mode");

				ThrowIfApplied();

				_mode = value;
			}
		}

		/// <summary>Gets or sets the string format applied to the bound value.</summary>
		/// <value>A standard <see cref="string.Format(string,object)" /> composite format string. For single-value bindings use one placeholder (e.g., <c>{0:C2}</c>).</value>
		/// <remarks>
		/// Used to format or composite the resulting bound value for display. Implementations follow standard <see cref="string.Format(string,object)" /> semantics.
		/// </remarks>
		public string StringFormat
		{
			get { return _stringFormat; }
			set
			{
				ThrowIfApplied();

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Validate the BindingMode against Enum.IsDefined(typeof(BindingMode), value) before assigning.
  2. Restrict the source of the value to a known set of constants or a dropdown of valid members.
  3. Coerce out-of-range values to BindingMode.Default as a safe fallback before assignment.

Example fix

// before
binding.Mode = (BindingMode)configInt;

// after
binding.Mode = Enum.IsDefined(typeof(BindingMode), configInt)
    ? (BindingMode)configInt
    : BindingMode.Default;
Defensive patterns

Strategy: validation

Validate before calling

static BindingMode SafeMode(BindingMode value) =>
    Enum.IsDefined(typeof(BindingMode), value) ? value : BindingMode.Default;

binding.Mode = SafeMode(incoming);

Type guard

static bool IsValidBindingMode(BindingMode m) =>
    m == BindingMode.Default || m == BindingMode.OneWay ||
    m == BindingMode.OneWayToSource || m == BindingMode.TwoWay ||
    m == BindingMode.OneTime;

Prevention

When it happens

Trigger: Assigning binding.Mode = (BindingMode)999 or any value outside the five defined members. Casting an arbitrary integer to BindingMode and passing it to a Binding. Setting Mode from deserialized/config data that permits out-of-range values.

Common situations: Deserializing a BindingMode from JSON/config where the numeric value is out of range. Reflection-based binding configuration that passes a raw enum value. Migrating from another framework whose binding direction enum has extra members that map to invalid MAUI values.

Related errors


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