Sub list is not binding to ListView grouping in xamarin forms

220 views Asked by At

I'm implementing listview grouping in xamarin forms to display the list within a listview. I need to show one header and a list under the header.

Models:

public class ActivityHistoryDetails
    {
        public string EventDate { get; set; }
        public List<History> History { get; set; }
    }
   

I'm binding a list of "ActivityHistoryDetails" type to the listview.

Now I'm able to display only the header with this code. Sub Lists are not binding.

1

There are 1 answers

5
Wen xu Li On BEST ANSWER

For your needs, you can refer to this link.

According to your code, I wrote a runnable, minimized example for your reference, you can refer to it to modify your code.

Here is the History class:

public class History
{
    public string ZoneId { get; set; }
    public string ZoneComment { get; set; }
    public string EventRptId { get; set; }
    public string EventDate { get; set; }
    public string EventTime { get; set; }
    public string EventDescription { get; set; }
    public int StoreId { get; set; }

    public History(string zoneId, string zoneComment, string eventRptId, string eventDate, string eventTime, string eventDescription, int storeId)
    {
        ZoneId = zoneId;
        ZoneComment = zoneComment;
        EventRptId = eventRptId;
        EventDate = eventDate;
        EventTime = eventTime;
        EventDescription = eventDescription;
        StoreId = storeId;
    }
}

Here is the ActivityHistoryDetails class:

public class ActivityHistoryDetails : List<History>
{
    public string EventDate { get; set; }
    public static IList<ActivityHistoryDetails> All { private set; get; }

    public ActivityHistoryDetails(string EventDate)
    {
        this.EventDate = EventDate;
    }

    static ActivityHistoryDetails() 
    {
        List<ActivityHistoryDetails> Groups = new List<ActivityHistoryDetails>
        {
            new ActivityHistoryDetails ("aaa")
            {
                new History("test","test","test","test","test","test",1),
                new History("test1","test1","test1","test1","test1","test1",2)
            },
            new ActivityHistoryDetails ("bbb")
            {
                new History("test2","test2","test2","test2","test2","test2",3),
                new History("test3","test3","test3","test3","test3","test3",4)
            }
        };
        All = Groups;
    }
}

Use GroupedView.ItemsSource = ActivityHistoryDetails.All; for data binding.

The xaml code is the same as the code you provided.

Here is the screenshot:

enter image description here