0
votes

I am setting ListView ItemSource to a List<T> where T is my Model. I am Binding some of the Property of this List<T> to some Label in XAML. And Now based on a Property, I want to Set Label to some Text.

For Example, if (Property.IsCompleted == true), I might want to set a Label in my View Cell in the ListView to "Done" instead of "True".

I hope this summarizes the problem. I have tried other things and none worked.

This is the Item Appearing Method of My ListView:


          private void bookingLV_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            BookingsModel convert = (BookingsModel)e.Item;
            var select = convert.IsCompleted;
         if(select == true)
            {
                IsDone = "Completed";
            }
            IsDone = "Pending";

        }

And I have a Custom Property called IsDone:

  public string IsDone { get; set; }

And This is how I am Binding IsDone in the View Cell of the ListView in Xaml

 <Label Text="{Binding IsDone}"></Label>

I want to be able to set the Text Property of my Label to some text based on a property of my Model Object.

1
either 1) use a ValueConverter, or 2) use a custom property on your model that returns the textJason
@Jason I did the number two as you can see from my question. You mind explaining this to me explicitly with an example?Classyk
IsDone does not appear to be a property of BookingsModelJason

1 Answers

0
votes

create a read only property in your model that returns a value based on another property

public string IsDone 
{ 
  get
    {
       if (select) return "Completed";
       return "Pending";
    }
}

if you are using INotifyPropertyChanged you will want to be sure that the setter of the "trigger" property fires PropertyChanged events for both

public bool selected { 
  get {
    ...
  }
  set {
    ...
    PropertyChanged("selected");
    PropertyChanged("IsDone");
  }
}