Change color of label if value of another is >0??? RRS feed

  • Question

  • User376350 posted

    Hello I am new to xamarin, i want to know how to change color of label if the value is higher than zero?

    If case 1 value of label is higher than zero then change color of label to red. if case 2 value of label is zero dont change color.

    Can somebody help me? Thanks

    Friday, August 24, 2018 9:04 AM

All replies

  • User371275 posted

    First you must name your label:

    then, in code behind do something like this

    if(variable > 0) { MyLabel.BackgroundColor = Xamarin.Forms.Color.Red; }

    Friday, August 24, 2018 1:41 PM
  • User350 posted

    This is using code behind. For Forms it's better to use the MVVM design pattern - so you would bind the Color property of the label to a property in the ViewModel. The getter of the ViewModel then returns the appropriate Color depending on the underlying value.

    Saturday, August 25, 2018 8:07 AM