Operation not supported on read-only collection

痴心易碎 提交于 2019-12-23 18:32:25

问题


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.


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/6422378/operation-not-supported-on-read-only-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!