While Binding Collection of Colors System.ArgumentException is occuring

I have a custom view in my PCL project in Xamarin.Forms. I am not able bind a collection of colors in Xaml to a bindable object in my CustomView.

I have set the binding as in below in xaml:

<local:CustomView x:Name="customView"  ColorPalette="{Binding Colors}"/>

My CustomView is as below:

public class CustomView : View
    public CustomView()


    public static void OnColorsChanged(BindableObject bindable, object oldValue, object newValue)
        // Some Code

    public static readonly BindableProperty ColorsPaletteProperty =
        BindableProperty.Create("ColorPalette", typeof(IEnumerable<Color>), typeof(CustomView), new List<Color>(){ Color.FromRgb(0, 0, 0),
            Color.FromRgb(251, 176, 59)}, BindingMode.Default, null, OnColorsChanged);

    public IEnumerable<Color> ColorPalette
        get { return (IEnumerable<Color>)GetValue(ColorsPaletteProperty); }
        set { SetValue(ColorsPaletteProperty, value); }

While performing the binding in Xaml, I get an exception "System.ArgumentException: Object of type 'Xamarin.Forms.Binding' cannot be converted to type 'Xamarin.Forms.Color'".

But when I bind the Colors in using SetBinding in code behind it is working properly.

Code Behind:

 //Binding using SetBinding is working where as {Binding Colors} in xaml is not working

customView.SetBinding<ViewModel>(CustomView.ColorsPaletteProperty, vm => vm.Colors);

Colors is a collection of colors of type IEnumerable / IList / List / ObservableCollection.

Any help is appreciated.



1 answer

  • answered 2018-01-17 17:26 Boggy

    A custom view embedded in another view will inherit the binding context of the parent view. Are you setting the binding context of the view to a viewmodel that has a Colors property? Some more code samples of where you're setting the binding context and of your viewmodel might be helpful.