AvaloniaUI/Avalonia · error · ArgumentException

Object of type '{value?.GetType()}' cannot be converted to t

Error message

Object of type '{value?.GetType()}' cannot be converted to type '{_property.PropertyType}'.

What it means

InpcPropertyAccessorPlugin.Accessor.SetValue writes a bound value by converting it to the property type via TypeUtilities.TryConvert. If conversion is impossible (no assignable or convertible relationship and no registered converter), it throws ArgumentException at binding-write time. This only fires on TwoWay/OneWayToSource bindings that actually push a value back.

Source

Thrown at src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs:128

            }

            public override Type? PropertyType => _property.PropertyType;

            public override object? Value
            {
                get
                {
                    var o = GetReferenceTarget();
                    return o != null ? _property.GetValue(o) : null;
                }
            }

            public override bool SetValue(object? value, BindingPriority priority)
            {
                if (_property.CanWrite)
                {
                    if (!TypeUtilities.TryConvert(_property.PropertyType, value, null, out var converted))
                        throw new ArgumentException($"Object of type '{value?.GetType()}' " +
                            $"cannot be converted to type '{_property.PropertyType}'.");

                    _eventRaised = false;
                    _property.SetValue(GetReferenceTarget(), converted);

                    if (!_eventRaised)
                    {
                        SendCurrentValue();
                    }

                    return true;
                }

                return false;
            }

            void IWeakEventSubscriber<PropertyChangedEventArgs>.
                OnEvent(object? notifyPropertyChanged, WeakEvent ev, PropertyChangedEventArgs e)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Add an IValueConverter that converts the source value to the target property type.
  2. Make the source property assignable/convertible to the target type.
  3. Use FallbackValue / TargetNullValue for null or unconvertible inputs.
  4. Set the binding mode to OneWay if a write-back is not required.

Example fix

<!-- before -->
<Binding Path="Age"/>  <!-- Age is int, source is string -->
<!-- after -->
<Binding Path="Age" Converter="{StaticResource StringToIntConverter}"/>
Defensive patterns

Strategy: validation

Validate before calling

// Before writing back, check TypeUtilities can convert the value:
if (!TypeUtilities.TryConvert(targetPropertyType, value, null, out var _))
{
    // supply a converter or skip the write
}

Type guard

// Guard the source value against the target property type before binding:
static bool CanConvertTo(Type? sourceType, Type targetType) =>
    sourceType is null || targetType.IsAssignableFrom(sourceType) ||
    TypeUtilities.TryConvert(targetType, Activator.CreateInstance(sourceType)!, null, out _);

Try / catch

try { accessor.SetValue(value, priority); }
catch (ArgumentException ex) when (ex.Message.Contains("cannot be converted"))
{
    // log, use FallbackValue, or report a BindingNotification
}

Prevention

When it happens

Trigger: Binding a string source to an int property where the string is not numeric; binding an enum to an incompatible type; null pushed into a non-nullable value-type property without a converter.

Common situations: Missing IValueConverter for a non-assignable type pair; culture-specific parse failures; null-to-value-type mismatches; OneWay binding accidentally set to TwoWay.

Related errors


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