C# 列表中嵌套对象的ListView绑定属性<;对象>;
我试图从列表中绑定一个名为“Name”的字符串属性,该列表由我在listView中绑定到的对象持有C# 列表中嵌套对象的ListView绑定属性<;对象>;,c#,xamarin.forms,C#,Xamarin.forms,我试图从列表中绑定一个名为“Name”的字符串属性,该列表由我在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 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; }
}
我有一个数据模板
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");
}
}
因此,所有内容都被正确绑定,除了Object.Name
没有显示在我的ListView中
我知道它不起作用,因为我的对象是一个列表我已经看到,我可以得到一个简单的数据列表,如下所示:
listOfMyObject.SelectMany(对象=>obj.Objects)
但我不知道该怎么办。
那么如何绑定MyObject列表中对象的属性呢
感谢公共类列表ToString Converter:IValueConverter
{
#区域转换器实现
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值!=null){
列表温度=(列表)值;
如果(临时计数==0)
返回“”;
字符串myString=“”;
foreach(临时对象对象对象){
myString+=obj.Name+“,”;
}
返回myString;
}
返回“”;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
#端区
}
问题在于对象是一个列表。因此Object.name是不明确的,因为它可以是对象列表中的任何名称。你将如何决定你使用的是哪一个对象?是的,我在写这篇文章时就知道。。。但我不知道该怎么做。我想在标签中显示所有的Name属性,比如Object.Name+“”+Object.Name+“”+Object.Name+“”+Object.Name+“”。所以我不想只显示列表中一个对象的属性名,而是显示所有对象的属性名_objectLabel.SetBinding(Label.TextProperty,“Objects[0].Name”);这是可行的,但如果我有3个对象,我想显示在我的评论上,我明白了,所以你想遍历所有对象并显示列表中的每个名称?是的,这正是我想做的。谢谢。如果有人想知道如何实现您的解决方案:_objectLabel.SetBinding(Label.TextProperty,“Objects”,BindingMode.Default,new ListToStringConverter());
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");
}
}
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
}