AvaloniaUI/Avalonia · error · InvalidOperationException

BindingValue has no value.

Error message

BindingValue has no value.

What it means

Thrown as InvalidOperationException by the BindingValue<T>.Value getter when HasValue is false. BindingValue<T> is a discriminated union (value / error / unset / donothing) and Value is only valid when the binding actually carries a value. Reading it on an error-only or unset binding is a contract violation.

Source

Thrown at src/Avalonia.Base/Data/BindingValue.cs:129

        public bool HasError => Type.HasAllFlags(BindingValueType.HasError);

        /// <summary>
        /// Gets a value indicating whether the binding value has a value.
        /// </summary>
        public bool HasValue => Type.HasAllFlags(BindingValueType.HasValue);

        /// <summary>
        /// Gets the type of the binding value.
        /// </summary>
        public BindingValueType Type { get; }

        /// <summary>
        /// Gets the binding value or fallback value.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// <see cref="HasValue"/> is false.
        /// </exception>
        public T Value => HasValue ? _value! : throw new InvalidOperationException("BindingValue has no value.");

        /// <summary>
        /// Gets the binding or data validation error.
        /// </summary>
        public Exception? Error { get; }

        /// <summary>
        /// Converts the binding value to an <see cref="Optional{T}"/>.
        /// </summary>
        /// <returns></returns>
        public Optional<T> ToOptional() => HasValue ? new Optional<T>(_value) : default;

        /// <inheritdoc/>
        public override string ToString() => HasError ? $"Error: {Error!.Message}" : _value?.ToString() ?? "(null)";

        /// <summary>
        /// Converts the value to untyped representation, using <see cref="AvaloniaProperty.UnsetValue"/>,
        /// <see cref="BindingOperations.DoNothing"/> and <see cref="BindingNotification"/> where

View on GitHub (pinned to 11c5427268)

Solutions

  1. Check HasValue before accessing Value, or use GetValueOrDefault() which returns default(T) safely.
  2. If you need the value-or-error, inspect Type first and branch (Value vs Error).
  3. Use ToOptional() to convert to Optional<T> and handle absence explicitly.

Example fix

// before
var v = bindingValue.Value; // throws if no value
// after
var v = bindingValue.HasValue ? bindingValue.Value : defaultValue;
// or
var v = bindingValue.GetValueOrDefault();
Defensive patterns

Strategy: validation

Validate before calling

if (bindingValue.HasValue)
    use(bindingValue.Value);
else
    use(bindingValue.GetValueOrDefault());

Type guard

static bool HasValue<T>(BindingValue<T> bv) => bv.HasValue;

Try / catch

try { var v = bindingValue.Value; }
catch (InvalidOperationException) { v = default; }

Prevention

When it happens

Trigger: Accessing bindingValue.Value when Type is UnsetValue, DoNothing, BindingError (without fallback), or DataValidationError (without fallback) — i.e., HasValue is false.

Common situations: Code reads .Value unconditionally after receiving a BindingValue from a binding pipeline without checking HasValue. Refactoring that dropped a HasValue guard. Treating BindingValue like a nullable without using GetValueOrDefault.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/c5199eea0d4f3348. Report an issue: GitHub.