2012-09-09 107 views
0

我試圖從GridView控件中刪除項目。當我點擊刪除時,GridView無法更新刪除項目GridView Windows 8地鐵

我已經綁定了GridView和可觀察集合中的訂單列表。這裏是我的訂單類:

public class Order : INotifyPropertyChanged 
{ 
    public Order() { } 
    private int _id; 
    [PrimaryKey, AutoIncrement] 
    public int Id 
    { 
     get { return _id; } 
     set 
     { 

      this._id = value; 
      Notify("ID"); 
     } 
    } 
    public int RestaurantID { get; set;} 
    public DateTime Date { get; set; } 
    public string Note { get; set; } 
    public decimal TotalPrice { get; set; } 
    [Ignore] 
    public List<OrderDetail> OrderDetails { get; set; } 

    #region INotifyPropertyChanged Members 

    [Ignore] 
    public event PropertyChangedEventHandler PropertyChanged; 

    [Ignore] 
    protected void Notify(string propName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
    #endregion 
} 

這裏是我的數據綁定代碼:

ObservableCollection<Order> orderCollection = new ObservableCollection<Order>(); 
      foreach (Order ord in orders) 
      { 
       orderCollection.Add(ord); 
      } 

      this.DefaultViewModel["Items"] = orderCollection; 

我的刪除按鈕在GridView的DataTemplate中存在。這是我的刪除代碼:

private void btnDelete_Click_1(object sender, RoutedEventArgs e) 
    { 
     Button btnDelete = (Button)sender; 
     Order order = btnDelete.DataContext as Order; 
     repository.DeleteOrder(order.Id); 
    } 

回答

0

它看起來像代碼只是從存儲庫中刪除項目。您還需要將其從ObservableCollection中刪除。嘗試在repository.DeleteOrder(order.Id)下加入:

orderCollection.Remove(order);