You can not use Ternary in XAML.
Solution 1 (Using Converter):
You can probably go with using ValueConverter. Create a Converter which implements IValueConverter.
public class ReadUnReadToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? isRead = Convert.ToBoolean(value);
if(isRead.HasValue && isRead.Value == true)
{
return Color.Grey;
}
return Color.White;
}
//You may not need the Convert Back method. This will need to convert Color back to Boolean
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
In your XAML Resource, add this class as a static resource, then you can use this converter to bind value and convert based on this.
<ContentPage.Resources>
<local:ReadUnReadToColorConverter x:Key="ReadUnReadToColorConverter" />
</ContentPage.Resources>
Now, Bind this Converter to your Grid as follows:
<Grid Margin="5,0,5,5" Padding="10" BackgroundColor="{Binding IsRead, Converter={StaticResource ReadUnReadToColorConverter}}">
Solution 2 (Using Property Binding (OneWay solution only)):
You can simply have a Color Property in your ViewModel and return value in the get method of the property based on the condition as follows:
public Color ReadUnReadBackgroundColor
{
get
{
if(IsRead.HasValue && IsRead.Value == true)
{
return Color.Grey;
}
return Color.White;
}
}
Now Bind this with Grid's BackgroundColor Property:
<Grid Margin="5,0,5,5" Padding="10" BackgroundColor="{Binding ReadUnReadBackgroundColor}">