2013-04-04 81 views
2

刪除項目需要一些幫助,當我點擊tap_event我得到一個消息框,刪除或取消其工程和價格帶下總卻一點兒也不更新後的購物車,它在「ListBoxCart.Items.Remove(CURR)崩潰,在此先感謝!WP7從Control通過消息框

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


    } 
+0

什麼是錯誤訊息? – Fabrice 2013-04-04 13:36:09

+0

InvalidOperationException未處理 – jennyH 2013-04-04 14:21:18

+0

抱歉,操作在只讀集合上不受支持。 – jennyH 2013-04-04 14:22:24

回答

0

我已經寫了artile(抱歉,在法國,但你可以閱讀XAML):http://www.peug.net/2012/05/17/contextmenu-dans-listbox-datatemplate/

,並在後臺代碼:一個例子:

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(); 
    } 
0

當您設置ItemsSource屬性列表框,它會生成一個read-only收集並顯示它們。什麼你試圖做的是訪問此只讀收集並修改它,而是因爲它是隻讀的,你不能這樣做。

相反,你可以有你的collecti在實施INotifyCollectionChanged接口,提高集合改變時,用戶已刪除的項目或使用ObservableCollection而不是儲存項目的事件。 ObservableCollection實現INotifyCollectionChanged接口給你,讓你可以從ObservableCollectionremove items和變化將在列表框中自動反映。

的ObservableCollection還實現INotifyPropertyChanged所以任何屬性更新也將在ListBox中更新。