Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Set ContentPage Background Color via Xaml & Converter

$
0
0

Hi,

I have a simple contentpage where I would like to change the background color based on a boolean value (false => background color red, true => green).

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:TopUpApp.Converters;assembly=FortytwoTech.Ecospace.Services.Clients.TopUpApp"
             x:Class="TopUpApp.Views.ResultView"
             Padding="10">
  <ContentPage.Resources>
    <ResourceDictionary>
      <Color x:Key="failedColor">Red</Color>
      <Color x:Key="successColor">#50E3AC</Color>
      <converters:StatusBackgroundConverter TrueValue="{StaticResource failedColor}" FalseValue="{StaticResource successColor}" x:Key="StatusBackgroundConverter"/>
    </ResourceDictionary>
  </ContentPage.Resources>
  <ContentPage.BackgroundColor>
    <Color Accent="{Binding IsSuccessful, Converter={StaticResource StatusBackgroundConverter}}"/>
  </ContentPage.BackgroundColor>
</ContentPage>

and my converter

    public class StatusBackgroundConverter : IValueConverter
    {
        public Color TrueValue { get; set; }
        public Color FalseValue { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var castedValue = bool.Parse(value.ToString());
            return (castedValue ? TrueValue : FalseValue);

        }

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

The error that is being thrown is: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Xamarin.Forms.Xaml.XamlParseException: Position 18:12. Cannot assign property "Accent": type mismatch between "Xamarin.Forms.Binding" and "Xamarin.Forms.Color"

Apparently it doesn't work as I thouught it would be, I tried the SolidBrushColor (wpf) but that is not available.

Any suggestions how I would do this?

tnx!


Viewing all articles
Browse latest Browse all 89864

Trending Articles