0

我试图从 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);
    }
4

1 回答 1

0

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

orderCollection.Remove(order);
于 2012-09-09T02:37:12.817 回答