You might need a ValueConverter . Now you set the background color to βPossible,β βNo,β or βYes,β which is clearly not a color.
What you need to do is convert this value to color. You can do it like this.
Create a new class that implements the IValueConverter interface. It probably looks something like this:
public class YesNoMaybeToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { switch(value.ToString().ToLower()) { case "yes": return Color.Green; case "no": return Color.Red; case "maybe": return Color.Orange; } return Color.Gray; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
Then add this class as a static resource to your XAML page as follows:
<ContentPage.Resources> <local:YesNoToBooleanConverter x:Key="YesNoMaybeToColorConverter" /> </ContentPage.Resources>
Now in your binding you have to tell him which converter to use. Do it like this:
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{Binding Result, Converter={StaticResource YesNoMaybeToColorConverter}}" />
Now he should see the value in the Result field, put it through the converter you specified and return the color that matches your value.
source share