Windows phone 7 WP7通过消息框从列表框中删除项目

Windows phone 7 WP7通过消息框从列表框中删除项目,windows-phone-7,listboxitems,Windows Phone 7,Listboxitems,需要一些帮助,当我单击tap_事件时,我会收到一个消息框delete或cancel,该消息框起作用,价格从总额中扣除,但它不会更新购物车,因为它在“ListBoxCart.Items.Remove(curr)”上崩溃,提前感谢 private void listBoxCart_Tap(object sender, GestureEventArgs e) { if (MessageBox.Show("Are you sure!", "Delete", Message

需要一些帮助,当我单击tap_事件时,我会收到一个消息框delete或cancel,该消息框起作用,价格从总额中扣除,但它不会更新购物车,因为它在“ListBoxCart.Items.Remove(curr)”上崩溃,提前感谢

    private void listBoxCart_Tap(object sender, GestureEventArgs e)
    {
        if (MessageBox.Show("Are you sure!", "Delete", MessageBoxButton.OKCancel)
            == MessageBoxResult.OK)
        {

            foreach (Dvd curr in thisapp.ShoppingCart)
            {
                if (curr.Equals(listBoxCart.SelectedItem))
                {
                    listBoxCart.Items.Remove(curr);
                    listBoxCart.SelectedIndex = -1;
                    total -= Convert.ToDecimal(curr.price);

                    NavigationService.Navigate(new Uri("/ShoppingCart.xaml", UriKind.RelativeOrAbsolute));
                }


            }
            txtBoxTotal.Text = total.ToString();
            listBoxCart.ItemsSource = thisapp.ShoppingCart;
        }
        else
        {
            NavigationService.Navigate(new Uri("/ShoppingCart.xaml", UriKind.RelativeOrAbsolute));
        }


    }

当您为列表框设置
ItemsSource
属性时,它会生成一个
只读
集合并显示它们。您要做的是访问此只读集合并对其进行修改,但由于它是只读的,因此无法执行此操作

相反,您可以让您的收藏实现
INotifyCollectionChanged
界面,并在用户删除项目时引发收藏更改事件,或者使用替代来存储您的项目。
observeCollection
为您实现
INotifyCollectionChanged
界面,以便您可以从ode>ObservableCollection,更改将自动反映在列表框中

ObservableCollection还实现了INotifyPropertyChanged,因此任何属性更新也将在列表框中更新。

我写了一篇artile(抱歉,用法语,但您可以阅读XAML):

在代码背后:一个例子:

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        var menuItem = sender as MenuItem;
        var fe = VisualTreeHelper.GetParent(menuItem) as FrameworkElement;
        Dvd _fig = fe.DataContext as Dvd;
        thisapp.ShoppingCart.Remove(_fig);

        reloading();
    }

错误消息是什么?InvalidOperationException未处理抱歉,只读集合不支持该操作。您无法从列表框中删除项。您需要从提供的项集合中删除项。@Fabrice感谢您的快速响应,我采取的方法完全错误,我只能从列中添加/删除项选择不是列表框,非常感谢