Samantha J T Star
Samantha J T Star

Reputation: 32758

How can I make a template binding that accepts a string for a color?

I am using a template like this:     

<template:ButtonTemplate ButtonType="2" Grid.Column="0" Text="{Binding FBtnText}" 
     LabelTextColor="{Binding FBtnLabelTextColor, Converter={StaticResource StringToColorConverter}" 
     TapCommand="{Binding FBtnCmd }" />

So the value of the color is entered as "#FF0000" and the converter then converts this into a color.

Is there a way that I could do this conversion in the binding itself so that I would not need to use the StringToColor converter?

Here's my binding that I am using right now:

public static readonly BindableProperty LabelTextColorProperty =
            BindableProperty.Create(
                nameof(LabelTextColor),
                typeof(Color),
                typeof(ButtonTemplate),
                Color.FromHex("C9C9C9"));

public Color LabelTextColor
{
    get { return (Color)GetValue(LabelTextColorProperty); }
    set { SetValue(LabelTextColorProperty, value); }
}

Upvotes: 0

Views: 73

Answers (1)

Daniel Cunha
Daniel Cunha

Reputation: 676

You don't need to use a Converter, Xamarin Forms accepts by default a string as a Color, you just have to use like this: "#XXXXXX". You could just pass "#FF0000" and it would be accepted.

Upvotes: 2

Related Questions