ObservableCollection的CollectionChanged没有开火

时间:2017-01-25 13:04:08

标签: c# observablecollection

我有ObservableCollection,基于List

private List<int> markers = new List<int>();
private ObservableCollection<int> markersObservable;

markersObservable = new ObservableCollection<int>(markers);
markersObservable.CollectionChanged += MarkersObservable_CollectionChanged;

将元素添加到列表中时:

markers.Add(hScrollBarPoints.Value);

我希望收集更改事件会触发,但事件从未触发过:

private void MarkersObservable_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    Console.WriteLine("Changed!");
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

当您向List添加项目时,ObservableCollection不会更改,因此不会触发CollectionChanged事件。

要触发事件,您必须将项目添加到ObservableCollection

markersObservable.Add(hScrollBarPoints.Value);