Operation not supported in C # wp7 read-only collection

I try to implement lazy "load more" elements when the user falls to the bottom of the list, but every time I try to add new elements to the list, I get the following results:

"The operation is not supported in the read-only collection.

I have already tried several solutions on the forums on blogs that don't seem to work. I can't even understand the logic behind the problem, which seems a little strange to me.

What I am doing is basically loading a list of items and assigning my list items as a source.

wineFilterListBox.ItemsSource = wines; 

When the user gets to the bottom of the list, I add more elements (like the twitter application for wp7)

 public ObservableCollection<Wine> wines; ... if (atBottom) { int Count = page.wineFilterListBox.Items.Count; int end = Count + 10; for (int i = Count; i < end; i++) { page.LoadWineList(Count); } } ... private void LoadWineList(int Count = 1) { ... wineFilterListBox.Items.Add(wines); } 
+7
source share
2 answers

When you use an ItemSource , the Items collection becomes read-only. It looks like you will have to add data to the collection, and not to the ListBox Items property.

See MSDN: ItemsControl.ItemSource Property

In particular, this section:

When the ItemsSource property is set, the Items collection is created for read-only and fixed size.

Try adding the item to the wines collection directly, since your collection is an "ObservableCollection":

You must set an ItemSource for an object that implements INotifyCollectionChanged so that changes to the collection are reflected in the ItemsControl. The ObservableCollection (Of T) class defines such an object.

+9
source

FYI, if you intentionally do not want to use ObservableCollection to set ItemsSource you can add Wine manually to your wineFilterListBox.Items

 for (int i = Count; i < 10; i++) { wineFilterListBox.Items.Add(new Wine()); } 

This will make wineFilterListBox.Items not read-only.

0
source

All Articles