3

I have a LongListSelector in a Windows Phone Silverlight project, that is bound to a nested ObservableCollection.

In order to get the grouping working and updating automatically, I'm using a custom group class that extends ObservableCollection.

My class structure looks like this:

Main.xaml.cs:

ObservableCollection<Group<MyViewModel>> _groups;

Group.cs:

Group<T> : ObservableCollection<T> {...}

I'm populating the groups asynchronously, using a WebClient:

WebClient wc = new WebClient();
wc.OpenReadCompleted += (sender, obj) {
    // parse the response here, get list of MyModels
    ...

    foreach (var model in models)
    {
        var group = _groups.SingleOrDefault(g => g.Key == model.Key);
        if (group == null)
        {
            group = new Group<MyModel> { Key = model.Key };
            _groups.Add(group);
        }

        group.Add(model);
    }
}

All this works fine, except for binding to the LongListSelector. The first item gets added fine, but every subsequent item added to the groups list results in an IndexOutOfBounds exception.

I've tried handling the CollectionChanged event to add the items to the group, instead of on ReadComplete, but same result.

Any help would be greatly appreciated.

munkay
  • 1,883
  • 2
  • 16
  • 15

1 Answers1

0

Are you trying to add each item as it comes as a group. You should get the entire data and group them and assign to the longlistselector. Groping on a incremental loaded data would not work good as a new item coming in might need to be added to an existing group.

Here is a good sample on the same to use on a flat list that is readily available and grouped and assigned to longlistselector

How to display data in a grouped list in LongListSelector for Windows Phone 8

Windows Phone Series – Jump Lists

Rahul P Nath
  • 324
  • 4
  • 9