0
votes

I'm trying to bind a string property called "Name" from a List < T > holded by the object I'm binding to in a listView.

public class MyObject 
{
    public List<Object> Objects{ get; set; }
    public string Description {get; set; }
    public string StartDate {get; set; }
    public string EndDate {get ;set; }
    public string Type {get; set; }

    public MyObject()
    {
        Objects = new List<Object>();
    }
}

public class Object
{
    public string Name { get; set; }
    public int? Id { get; set; }
    public int? Order { get; set; }
}

In my Page, I set ListView.ItemSource from call async which is a a List<MyObject>

 var itemSource = listOfMyObject;

I got a DataTemplate

public class Cell : ViewCell     
{
    private void SetBindings()
    {
        _objectLabel.SetBinding(Label.TextProperty, "Object.Name");
        _descriptionLabel.SetBinding(Label.TextProperty, "Description");
        _dateStartLabel.SetBinding(Label.TextProperty, "StartDate");
        _dateEndLabel.SetBinding(Label.TextProperty, "EndDate");
        _typeOfRequest.SetBinding(Label.TextProperty, "Type");
    }
}

So everything is bound correctly, except for Object.Name which does not display in my ListView.

I know it doesn't work because my Objects is a List< T > and does not have the Name property. Well, but how can I achieve what I want ? And I do not want to use nested listView just for one label.
I've seen that I can get a flat list of the data with something like : listOfMyObject.SelectMany(obj => obj.Objects)

but don't know what do of that. So how could I bind the property of the Object in the List of MyObject ?

Thanks

1
The issue is that Object is a list. So Object.name is ambiguous, because it could be any name in the list of objects. How will you decide which object it is you are working with? - Megadec
Yep I know that as I wrote it ... but I don't see then how to do it. Well and I'd like to display all Name property in the label, like Object.Name + " " + Object.Name + " " + Object.Name + " " . So I don't want to display just the property Name of one object in List but all of them - Umar3x
_objectLabel.SetBinding(Label.TextProperty, "Objects[0].Name"); That is working but if I got like 3 objects I'd like to display as I show on my preivous comment - Umar3x
Ahh I see, so you want to iterate through all objects and display each name in the list? - Megadec
Yes that's exactly what I want to do - Umar3x

1 Answers

1
votes
public class ListToStringConverter : IValueConverter
{

    #region IValueConverter implementation

    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value!= null)  {
           List<Object> temp = (List<Object>)value;
           if(temp.Count == 0 )
                return "";

           string myString = "";
           foreach(Object obj in temp){
               myString += obj.Name + ",";
          }

          return myString;
        }
        return "";
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException ();
    }

    #endregion
}