Reputation: 309
I have a ListBox with rows where each row consist of an "Image" and a "TextBlock". When I delete one row in the back with code like:
this.UserListBox.Items.RemoveAt(this.UserListBox.SelectedIndex);
There it throws an exception:
Operation not supported on read-only collection.
How can I delete row from the listbox?
I am writing a Windows phone 7 APP.
Upvotes: 4
Views: 14033
Reputation: 16043
Instead of binding a List of items to your list box's itemsSource, you should instead use an ObservableCollection. This will fix the problem. The ObservabeCollection has a Remove method that you can use
UserListBox.Items.Remove(this.UserListBox.SelectedItem);
Upvotes: 0
Reputation: 1515
If you set ItemsSource on the ListBox, then Items is internally generated and readonly. In such case you need to delete the item from the supoplied item collection. If the collection implements INotifyCollectionChanged, then the collection changes are reflected in the listbox.
Upvotes: 5