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.