how to change custom render label textcolor inside list view in xamarin forms

Viewed 114

We are developing a small application, we have created dashboard using custom render but I can’t change label text color. it is default showing like lable text color white, list view background color It will come via api so that if it is coming white background then label text color is not able to see. Here I have attached the code below. Give me suggestions to resolve this issue

Menucontrol custom render

public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create<MenuControl, IEnumerable>(
                view => view.ItemsSource,
                null,
                BindingMode.TwoWay,
                null,
                propertyChanged: (bindableObject, oldValue, newValue) =>
                {
                    ((MenuControl)bindableObject).ItemsSourceChanged(bindableObject, oldValue, newValue);
                }
            );

        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);

            }
        }
1 Answers

Add a Data Trigger

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>
       <ViewCell>
          <StackLayout BackgroundColor={Binding BGColor}>
              <Label TextColor="White">
                 <Label.Triggers>
                  <!--(or Value ="White" depends on binding value Xamarin.Color or string) -->
                    <DataTrigger TargetType="Label" Binding={Binding BGColor} Value="#FFFFFF">
                        <Setter Property="TextColor" Value="Red"/>
                                                 <!--(or your color) -->
                    </DataTrigger>
                 </Label.Triggers>
              </Label>
          </StackLayout>
       <ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

So when your BGColor(or any property you bind to color) property is something you think that can clash(for e.g. white) with your text color(e.g. also white) use data triggers. You can create multiple but if there are more than 3 or 4 I would advise you to use converters in that case.

Related