2017-10-05 61 views
0

我想绑定一个名为“Name”的名称来自列表< T>的字符串属性,该列表由我在ListView中绑定的对象支配。列表中的嵌套对象的ListView绑定属性<Object>

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; } 
    } 

在我的网页我设置的呼叫异步我的ListView AA级列表<为MyObject>

var itemSource = listOfMyObject; 

我有一个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"); 
     } 
} 

所以一切都是正确绑定的的ItemSource ,除了在我的listView中不显示的“Object.Name”。

我知道这是行不通的,因为我的对象是一个List < T>并没有Name属性。那么,但我怎么能达到我想要的?我不想只为一个标签使用嵌套的listView。
我已经看到了,我可以得到的数据的平面列表的东西,如: listOfMyObject.SelectMany(obj => obj.Objects)

,但不知道什么做的。 那么我怎样才能绑定对象的属性列表<对象>的MyObject?

感谢

+0

的问题是,对象是一个列表。所以Object.name是不明确的,因为它可以是对象列表中的任何名称。你将如何决定你正在使用哪个对象? – Megadec

+0

是的,我知道,就像我写的那样......但我不知道该怎么做。 嗯,我想显示标签中的所有名称属性,如Object.Name +“”+ Object.Name +“”+ Object.Name +“”。所以我不想只显示列表中的一个对象的属性名称,但它们都是 –

+0

_objectLabel.SetBinding(Label.TextProperty,“Objects [0] .Name”); 这是工作,但如果我有3个对象我想要显示,因为我显示在我的意见 –

回答

1
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 
    } 
+0

工作。非常感谢。如果有人想知道如何实现你的解决方案: _objectLabel.SetBinding(Label.TextProperty,“Objects”,BindingMode.Default,new ListToStringConverter()); –