[英]ListView binding property of nested object in List<Object>
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. 我正在尝试从要绑定到listView的对象所持有的List <T>中绑定一个名为“名称”的字符串属性。
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 aa List<MyObject>
在我的页面中,我通过调用async设置ListView.ItemSource
,它是一个List<MyObject>
var itemSource = listOfMyObject;
I got a DataTemplate 我有一个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. 因此,除了没有显示在我的ListView中的Object.Name
之外,其他所有内容Object.Name
正确绑定。
I know it doesn't work because my Objects is a List< T > and does not have the Name property. 我知道它不起作用,因为我的对象是List <T>,并且没有Name属性。 Well, but how can I achieve what I want ? 好吧,但是我怎样才能实现我想要的呢? And I do not want to use nested listView just for one label. 而且我不想仅将嵌套listView用于一个标签。
I've seen that I can get a flat list of the data with something like : listOfMyObject.SelectMany(obj => obj.Objects)
我已经看到我可以使用以下内容来获取数据的平面列表: 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 ? 那么,如何绑定MyObject列表中Object的属性?
Thanks 谢谢
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.