{"record":{"id":"5810aec165451743","repo":"AvaloniaUI/Avalonia","slug":"object-of-type-value-gettype-cannot-be-conv","errorCode":null,"errorMessage":"Object of type '{value?.GetType()}' cannot be converted to type '{_property.PropertyType}'.","messagePattern":"Object of type '(.+?)' cannot be converted to type '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs","lineNumber":128,"sourceCode":"            }\n\n            public override Type? PropertyType => _property.PropertyType;\n\n            public override object? Value\n            {\n                get\n                {\n                    var o = GetReferenceTarget();\n                    return o != null ? _property.GetValue(o) : null;\n                }\n            }\n\n            public override bool SetValue(object? value, BindingPriority priority)\n            {\n                if (_property.CanWrite)\n                {\n                    if (!TypeUtilities.TryConvert(_property.PropertyType, value, null, out var converted))\n                        throw new ArgumentException($\"Object of type '{value?.GetType()}' \" +\n                            $\"cannot be converted to type '{_property.PropertyType}'.\");\n\n                    _eventRaised = false;\n                    _property.SetValue(GetReferenceTarget(), converted);\n\n                    if (!_eventRaised)\n                    {\n                        SendCurrentValue();\n                    }\n\n                    return true;\n                }\n\n                return false;\n            }\n\n            void IWeakEventSubscriber<PropertyChangedEventArgs>.\n                OnEvent(object? notifyPropertyChanged, WeakEvent ev, PropertyChangedEventArgs e)","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/Plugins/InpcPropertyAccessorPlugin.cs#L110-L146","documentation":"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.","triggerScenarios":"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.","commonSituations":"Missing IValueConverter for a non-assignable type pair; culture-specific parse failures; null-to-value-type mismatches; OneWay binding accidentally set to TwoWay.","solutions":["Add an IValueConverter that converts the source value to the target property type.","Make the source property assignable/convertible to the target type.","Use FallbackValue / TargetNullValue for null or unconvertible inputs.","Set the binding mode to OneWay if a write-back is not required."],"exampleFix":"<!-- before -->\n<Binding Path=\"Age\"/>  <!-- Age is int, source is string -->\n<!-- after -->\n<Binding Path=\"Age\" Converter=\"{StaticResource StringToIntConverter}\"/>","handlingStrategy":"validation","validationCode":"// Before writing back, check TypeUtilities can convert the value:\nif (!TypeUtilities.TryConvert(targetPropertyType, value, null, out var _))\n{\n    // supply a converter or skip the write\n}","typeGuard":"// Guard the source value against the target property type before binding:\nstatic bool CanConvertTo(Type? sourceType, Type targetType) =>\n    sourceType is null || targetType.IsAssignableFrom(sourceType) ||\n    TypeUtilities.TryConvert(targetType, Activator.CreateInstance(sourceType)!, null, out _);","tryCatchPattern":"try { accessor.SetValue(value, priority); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"cannot be converted\"))\n{\n    // log, use FallbackValue, or report a BindingNotification\n}","preventionTips":["Provide an IValueConverter for non-assignable source/target type pairs.","Match source and target property types where possible.","Use FallbackValue/TargetNullValue for null or unconvertible inputs.","Prefer OneWay bindings when write-back conversion is uncertain."],"tags":["binding","type-conversion","inpc","two-way","binding"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}