Search code examples
c#xamlbindingtextcolor

Xamarin Forms - Label TextColor doesn't update by Binding


So, basically, I have a list of Button. When you click on one of them, then its Label TextColor changes. To make it, I'm using {Binding value} however, it doesn't work.. I tried to throw an event but.. nothing happens.. I tried to set it directly from the Label and it works.. why?

I have this code in the C# part:

private Color GroupSuperModLabelColor { get; set; }
private void OnGroupsClicked(object sender, EventArgs ea)
{
    if (GroupSuperModLabelColor == App.NL_White)
        GroupSuperModLabelColor = App.NL_OrangeBeer;
    else
        GroupSuperModLabelColor = App.NL_White;
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GroupSuperModLabelColor)));

    //If I don't add this line, then it doesn't work, WHY?
    GroupLabel.TextColor = GroupSuperModLabelColor;

    Debug.WriteLine("OnGroupsClicked clicked !");
}

There is the declaration of the XAML part:

<AbsoluteLayout HeightRequest="{Binding SuperModHeightSize}" WidthRequest="{Binding SuperModWidthSize}">
  <control:CustomLabel x:Name="GroupLabel" Text="Groups" FontFamily="{extension:FontFamily Roboto_Light}" FontSize="20" TextColor="{Binding GroupSuperModLabelColor}"
                       HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
                       AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                       AbsoluteLayout.LayoutFlags="All"/>
  <control:CustomButton BackgroundColor="Transparent" BorderColor="Transparent"
                        Clicked="OnGroupsClicked" LongPress="OnGroupsLongPress"
                        AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                        AbsoluteLayout.LayoutFlags="All"/>
  </AbsoluteLayout>

My Binding context is set to this and every other Binding things works.. It's just that with color, it doesn't seems to work..

Why? Thank in advance


Solution

  • Your GroupSuperModLabelColor isn't declared as a bindable property: https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/

    You have to change the declaration.