CommunityToolkit / CommunityToolkit/Maui
[BUG] BaseConverterOneWay don't work with Bindings in MultiBinding
- Dominant language
- C#
- Stars
- 2.7k
- Forks
- 500
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 7
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
### Current Behavior
When using a `BaseConverterOneWay` on a Binding in a MultiBinding a `System.ArgumentException: targetType needs to be assignable from System.String. (Parameter 'targetType')` is thrown from `ValueConverterExtension.ValidateTargetType`.
xaml
```xaml
```
Converter
```C#
public class IntToStringConverter : BaseConverterOneWay
{
public override string ConvertFrom(int value, CultureInfo? culture)
{
return value.ToString(culture);
}
public override string DefaultConvertReturnValue { get; set; } = "NULL";
}
```
### Expected Behavior
As `Count` is an int, I would expect a `BaseConverterOneWay` to convert it from int to string.
I'm not sure if this is intended behavior, but when using a `BaseConverterOneWay` with a MultiBinding the converter works as expected. Also when `ConvertFrom()` returns a string.
### Steps To Reproduce
The repro contains two converters, one ``, which is the one causing the bug, and one `` which is the workaround. The two converters are used in `MainPage.xaml`.
```xaml
```
Uncomment the `IntToStringConverter` to reproduce the bug. The app will crash on startup, but in debug the exception is catched and can be inspected.
All help is appreciated!
### Link to public reproduction project repository
https://github.com/jorgenmyrvold/BaseConverterOneWayMultiBindingBug
### Environment
```markdown
- .NET MAUI CommunityToolkit: 11.0.0
- OS: Android API 35 (Havn't tried on other OS)
- .NET MAUI: 9.0
```
### Anything else?
Stacktrace:
```
System.ArgumentException: targetType needs to be assignable from System.String. (Parameter 'targetType')
at CommunityToolkit.Maui.Extensions.ValueConverterExtension.ValidateTargetType[String](Type targetType, Boolean shouldAllowNullableValueTypes) in /_/src/CommunityToolkit.Maui/Extensions/ValueConverterExtension.shared.cs:line 66
at CommunityToolkit.Maui.Converters.BaseConverter`2[[System.Int32, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) in /_/src/CommunityToolkit.Maui/Converters/BaseConverter.shared.cs:line 180
at CommunityToolkit.Maui.Converters.ICommunityToolkitValueConverter.Microsoft.Maui.Controls.IValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) in /_/src/CommunityToolkit.Maui/Converters/ICommunityToolkitValueConverter.shared.cs:line 57
at Microsoft.Maui.Controls.Binding.GetSourceValue(Object value, Type targetPropertyType) in /_/src/Controls/src/Core/Binding.cs:line 183
at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindingExpression.cs:line 168
at Microsoft.Maui.Controls.BindingExpression.Apply(Object sourceObject, BindableObject target, BindableProperty property, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindingExpression.cs:line 94
at Microsoft.Maui.Controls.Binding.Apply(Object context, BindableObject bindObj, BindableProperty targetProperty, Boolean fromBindingContextChanged, SetterSpecificity specificity) in /_/src/Controls/src/Core/Binding.cs:line 150
at Microsoft.Maui.Controls.BindableObject.ApplyBindings(Boolean fromBindingContextChanged) in /_/src/Controls/src/Core/BindableObject.cs:line 693
at Microsoft.Maui.Controls.BindableObject.BindingContextPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue) in /_/src/Controls/src/Core/BindableObject.cs:line 734
at Microsoft.Maui.Controls.BindableObject.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean didChange, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/BindableObject.cs:line 676
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/Element/Element.cs:line 642
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in /_/src/Controls/src/Core/BindableObject.cs:line 663
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindableObject.cs:line 591
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 496
at Microsoft.Maui.Controls.BindableObject.set_BindingContext(Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 58
at Microsoft.Maui.Controls.MultiBinding.Apply(Object context, BindableObject targetObject, BindableProperty targetProperty, Boolean fromBindingContextChanged, SetterSpecificity specificity) in /_/src/Controls/src/Core/MultiBinding.cs:line 167
at Microsoft.Maui.Controls.BindableObject.ApplyBindings(Boolean fromBindingContextChanged) in /_/src/Controls/src/Core/BindableObject.cs:line 693
at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 367
at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context) in /_/src/Controls/src/Core/Element/Element.cs:line 801
at Microsoft.Maui.Controls.Element.b__94_0(BindableObject child, Object bc) in /_/src/Controls/src/Core/Element/Element.cs:line 577
at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext) in /_/src/Controls/src/Core/BindableObjectExtensions.cs:line 48
at Microsoft.Maui.Controls.Element.OnBindingContextChanged() in /_/src/Controls/src/Core/Element/Element.cs:line 575
at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged() in /_/src/Controls/src/Core/VisualElement/VisualElement.cs:line 1264
at Microsoft.Maui.Controls.View.OnBindingContextChanged() in /_/src/Controls/src/Core/View/View.cs:line 263
at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 368
at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context) in /_/src/Controls/src/Core/Element/Element.cs:line 801
at Microsoft.Maui.Controls.Element.b__94_0(BindableObject child, Object bc) in /_/src/Controls/src/Core/Element/Element.cs:line 577
at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext) in /_/src/Controls/src/Core/BindableObjectExtensions.cs:line 48
at Microsoft.Maui.Controls.Element.OnBindingContextChanged() in /_/src/Controls/src/Core/Element/Element.cs:line 575
at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged() in /_/src/Controls/src/Core/VisualElement/VisualElement.cs:line 1264
at Microsoft.Maui.Controls.View.OnBindingContextChanged() in /_/src/Controls/src/Core/View/View.cs:line 263
at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 368
at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context) in /_/src/Controls/src/Core/Element/Element.cs:line 801
at Microsoft.Maui.Controls.TemplatedPage.SetChildInheritedBindingContext(Element child, Object context) in /_/src/Controls/src/Core/TemplatedPage.cs:line 43
at Microsoft.Maui.Controls.Element.b__94_0(BindableObject child, Object bc) in /_/src/Controls/src/Core/Element/Element.cs:line 577
at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext) in /_/src/Controls/src/Core/BindableObjectExtensions.cs:line 48
at Microsoft.Maui.Controls.Element.OnBindingContextChanged() in /_/src/Controls/src/Core/Element/Element.cs:line 575
at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged() in /_/src/Controls/src/Core/VisualElement/VisualElement.cs:line 1264
at Microsoft.Maui.Controls.Page.OnBindingContextChanged() in /_/src/Controls/src/Core/Page/Page.cs:line 488
at Microsoft.Maui.Controls.ContentPage.OnBindingContextChanged() in /_/src/Controls/src/Core/ContentPage/ContentPage.cs:line 44
at Microsoft.Maui.Controls.BindableObject.BindingContextPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue) in /_/src/Controls/src/Core/BindableObject.cs:line 735
at Microsoft.Maui.Controls.BindableObject.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean didChange, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/BindableObject.cs:line 676
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/Element/Element.cs:line 642
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in /_/src/Controls/src/Core/BindableObject.cs:line 663
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindableObject.cs:line 591
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 496
at Microsoft.Maui.Controls.BindableObject.set_BindingContext(Object value) in /_/src/Controls/src/Core/BindableObject.cs:line 58
at BaseConverterOneWayMultiBindingBug.MainPage..ctor() in /Users/jorgen.myrvold/dev/BaseConverterOneWayMultiBindingBug/BaseConverterOneWayMultiBindingBug/MainPage.xaml.cs:line 8
at BaseConverterOneWayMultiBindingBug.App.CreateWindow(IActivationState activationState) in /Users/jorgen.myrvold/dev/BaseConverterOneWayMultiBindingBug/BaseConverterOneWayMultiBindingBug/App.xaml.cs:line 12
at Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.CreateWindow(IActivationState activationState) in /_/src/Controls/src/Core/Application/Application.cs:line 446
at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState) in /_/src/Core/src/Platform/Android/ApplicationExtensions.cs:line 46
at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState) in /_/src/Core/src/Platform/Android/MauiAppCompatActivity.cs:line 28
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net9.0/android-35/mcw/Android.App.Activity.cs:line 3196
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
```
Contributor guide
Assessment
This issue has not been assessed yet.