How to remove items from a list on a Windows 7 phone?

I use ItemsSource, when I want to remove an item from my list, I get an error. Error: The operation is not supported in the read-only collection.

MenuItem menuItem = (MenuItem) sender; MessageBox.Show(menuItem.Header.ToString(), "Result", MessageBoxButton.OK); ListBoxItem lb (ListBoxItem)listBoxJournal.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext); liste.Remove((Note)lb.Content); listBoxJournal.UpdateLayout(); listBoxJournal.Items.Clear(); listBoxJournal.ItemsSource = liste; 

I get an error in the items.clear line. Thanks

+6
source share
2 answers

If you set the ItemSource to a ListBox, the items will be internally created and read-only. Therefore, in this case, you need to remove the element from the collection of super-threads. If the collection implements INotifyCollectionChanged, then changes to the collection are reflected in the list.

ObservableCollection is a list that implemented INotifyCollectionChanged

+4
source

use this: Listname.RemoveAt (ListBoxName.SelectedIndex);

+4
source

Source: https://habr.com/ru/post/910477/


All Articles