使用ChangeTracking库,使用Add方法添加项目时,调用CollectionChanged,但是使用Insert方法将项目添加到集合中时,CollectionChanged从未调用!
var order = new Order
{
Id = 1,
CustomerNumber = "Test",
Address = new Address
{
AddressId = 1,
City = "New York"
},
OrderDetails = new ObservableCollection<OrderDetail>
{
new OrderDetail
{
OrderDetailId = 1,
ItemNo = "Item123"
},
new OrderDetail
{
OrderDetailId = 2,
ItemNo = "Item369"
}
}
};
TrackableOrder = order.AsTrackable();
((INotifyCollectionChanged)TrackableOrder.OrderDetails).CollectionChanged += OnCollectionChanged;
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//called on add but not On Insert
}
添加从未调用Insert OnCollectionChanged的项目,因此不会通知UI
TrackableOrder.OrderDetails.Insert(0, new OrderDetail()
{
ItemNo = $"{Guid.NewGuid()}",
OrderDetailId = 100
});
添加名为Add OnCollectionChanged的项目
TrackableOrder.OrderDetails.Add(new OrderDetail()
{
ItemNo = $"{Guid.NewGuid()}",
OrderDetailId = 200
});
任何帮助将不胜感激