简体   繁体   English

List中嵌套对象的ListView绑定属性<Object>

[英]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
}

排序列表<object>按嵌套列表<object>财产<div id="text_translate"><p>我有一个添加到另一个对象列表中的对象列表。 <strong>如何按对象子列表中的属性对列表进行排序?</strong> 请参阅下面的 SUDO 代码。 任何帮助将非常感激。 C#</p><pre> List&lt;object&gt; MainListOfObject = new List&lt;object&gt;(); List&lt;object&gt; subListObject_0 = new List&lt;object&gt;(); List&lt;object&gt; subListObject_1 = new List&lt;object&gt;(); List&lt;object&gt; subListObject_2 = new List&lt;object&gt;(); cat_a = {Age: 3, Colour: "white"} cat_b = {Age: 2, Colour: "black"} cat_c = {Age: 2.5, Colour: "black &amp; white"} dog_a = {Height: 0.5, EyeColour: "green"} dog_b = {Height: 0.6, EyeColour: "blue"} dog_c = {Height: 0.2, EyeColour: "brown"} Console.WriteLine(cat_a.Age); //output 1 Console.WriteLine(cat_b.Colour); //output "black" subListObject_0.Add(cat_a); subListObject_0.Add(dog_a); subListObject_1.Add(cat_b); subListObject_1.Add(dog_b); subListObject_2.Add(cat_c); subListObject_2.Add(dog_c); MainListOfObject.Add(subListObject_0); MainListOfObject.Add(subListObject_1); MainListOfObject.Add(subListObject_2); //How do I sort MainListOfObject by nested object property Age?</pre><p> ??? MainListOfObject.OrderBy(o[0] =&gt; o[0].Age).ToList(); ???</p><pre> Console.WriteLine(MainListOfObject[0][0].Age); //output 2 Console.WriteLine(MainListOfObject[1][0].Age); //output 2.5 Console.WriteLine(MainListOfObject[2][0].Age); //output 3</pre></div></object></object> - Sort List<object> by nested List<object> property

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将listview绑定到对象列表中的对象属性 - binding a listview to a property of an object in a list of objects 绑定到自定义对象的嵌套属性 - Binding to nested Property of Custom Object Xamarin:将嵌套集合对象绑定到 ListView - Xamarin: Binding a nested collection object to ListView Xamarin:将类对象属性绑定到ListView文本 - Xamarin: Binding Class Object property to ListView Text 排序列表<object>按嵌套列表<object>财产<div id="text_translate"><p>我有一个添加到另一个对象列表中的对象列表。 <strong>如何按对象子列表中的属性对列表进行排序?</strong> 请参阅下面的 SUDO 代码。 任何帮助将非常感激。 C#</p><pre> List&lt;object&gt; MainListOfObject = new List&lt;object&gt;(); List&lt;object&gt; subListObject_0 = new List&lt;object&gt;(); List&lt;object&gt; subListObject_1 = new List&lt;object&gt;(); List&lt;object&gt; subListObject_2 = new List&lt;object&gt;(); cat_a = {Age: 3, Colour: "white"} cat_b = {Age: 2, Colour: "black"} cat_c = {Age: 2.5, Colour: "black &amp; white"} dog_a = {Height: 0.5, EyeColour: "green"} dog_b = {Height: 0.6, EyeColour: "blue"} dog_c = {Height: 0.2, EyeColour: "brown"} Console.WriteLine(cat_a.Age); //output 1 Console.WriteLine(cat_b.Colour); //output "black" subListObject_0.Add(cat_a); subListObject_0.Add(dog_a); subListObject_1.Add(cat_b); subListObject_1.Add(dog_b); subListObject_2.Add(cat_c); subListObject_2.Add(dog_c); MainListOfObject.Add(subListObject_0); MainListOfObject.Add(subListObject_1); MainListOfObject.Add(subListObject_2); //How do I sort MainListOfObject by nested object property Age?</pre><p> ??? MainListOfObject.OrderBy(o[0] =&gt; o[0].Age).ToList(); ???</p><pre> Console.WriteLine(MainListOfObject[0][0].Age); //output 2 Console.WriteLine(MainListOfObject[1][0].Age); //output 2.5 Console.WriteLine(MainListOfObject[2][0].Age); //output 3</pre></div></object></object> - Sort List<object> by nested List<object> property 自定义ListView将其绑定到对象列表[winforms] - Custom ListView binding it to a object list [winforms] WinForms DataGridView:使用嵌套对象属性(扩展列)绑定对象 - WinForms DataGridView: Binding an object with a nested object property (expanding columns) 如何在列表视图中显示对象的List属性 - How to display object's List property in listview 将ListView绑定到列表 <T> 或清单 <InterfaceOfT> 与对象类型 - Binding ListView to List<T> or List<InterfaceOfT> with object type 从嵌套对象列表中获取属性值 - Get Property Value From Nested Object List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM