如何在Windows 7手机上从列表框中删除项目?

6

我使用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;

我在items.clear这一行遇到了错误。谢谢。

2个回答

4
如果您在ListBox上设置了ItemsSource,则Items是内部生成的且只读的。因此,在这种情况下,您需要从提供的项集合中删除该项。 如果集合实现了INotifyCollectionChanged接口,则集合更改将反映在listbox中。
ObservableCollection是一个实现了INotifyCollectionChanged接口的列表。

4
使用以下代码 --- Listname.RemoveAt(ListBoxName.SelectedIndex);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接