2
votes

Hello Guys i need help.

i am using a converter on a binding to set a background color depending on an object id.(stacklayout inside contentview)

<StackLayout.BackgroundColor>
     <Binding Path="objectID" Converter="{StaticResource IntToColorConverter}"/>
</StackLayout.BackgroundColor>

This works. Now i want to use the multiconverter (new in Xamarin 4.7) to return a different backgroundcolor depending on other object properties.(for context: object is a calendarentry and if its in the past it should be desaturated or something)

<StackLayout.BackgroundColor>
            <MultiBinding Converter="{StaticResource MultiColorConverter}">
                <Binding Path="objectID"/>
                <Binding Path="value"/>
                <Binding Path="value2"/>
            </MultiBinding>
</StackLayout.BackgroundColor>

This does not work, since the values that are given to the converter are all NULL and the color becomes black (the return value if all vslues are NULL; so the converter is set up correctly also). It also shows this when i use a break point on the converter, that the array contains only NULL variables.

I don't know what i am missing here, the bindingcontext should be inheritated and does not change. Any hint would be appreciated.

The bindingcontext is set programatically on a contentpage on creation of the ContentView where i provide an object from a list of objects.

var evnt = new TimeTableEventView { BindingContext = Model.calenderevents[j] };
1

1 Answers

1
votes

You need to return BindableProperty.UnsetValue to use the binding FallbackValue .

in xaml

<StackLayout.BackgroundColor>
            <MultiBinding Converter="{StaticResource MultiColorConverter}">
                <Binding Path="red"/>
                <Binding Path="green"/>
                <Binding Path="blue"/>
               
            </MultiBinding>
</StackLayout.BackgroundColor>

in Converter

public class MultiColorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (var value in values)
        {
            if (!(value is int b))
            {
                return Color.White;
                // set a default value when unset
            }
           
        }

        int red = (int)values[0];
        int green = (int)values[1];
        int blue = (int)values[2];

        Color color = Color.FromRgb(red,green,blue);

        return color;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

in code behind

public class MyViewModel
    {
        public int red { get; set; }
        public int green { get; set; }
        public int blue { get; set; }

        public MyViewModel(int r, int g, int b)
        {
            red = r;
            green = g;
            blue = b;
        }

    }
BindingContext = new MyViewModel(120, 60, 180);

enter image description here