我使用ItemsSource,当我想从列表中删除一个项目时,我收到一个错误.错误:只读集合不支持操作.
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;
Run Code Online (Sandbox Code Playgroud)
我在行items.clear上得到了错误.谢谢
小智 4
如果在 ListBox 上设置 ItemsSource,则 Items 是内部生成的并且是只读的。因此,在这种情况下,您需要从提供的项目集合中删除该项目。如果集合实现 INotifyCollectionChanged,则集合更改将反映在列表框中。
ObservableCollection 是一个实现了 INotifyCollectionChanged 的列表