将项目插入可跟踪集合时不触发CollectionChanged

时间:2018-09-06 17:26:24

标签: c# proxy change-tracking

使用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
});

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

这是库中的错误,已修复, 子级集合跳过插入+其他方法: https://github.com/joelweiss/ChangeTracking/pull/44