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