删除项目GridView Windows 8 Metro

时间:2012-09-09 02:33:10

标签: c# xaml microsoft-metro

我试图从GridView控件中删除项目。当我单击删除时,GridView无法更新

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

1 个答案:

答案 0 :(得分:0)

看起来代码只是从存储库中删除项目。您还需要将其从ObservableCollection中删除。尝试在repository.DeleteOrder(order.Id)下添加此内容:

orderCollection.Remove(order);