简体   繁体   中英

ObservableCollection's CollectionChanged not firing

I have an ObservableCollection , based on List .

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

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

When adding element into the list:

markers.Add(hScrollBarPoints.Value);

I expect collection changed event would fire, however, event never fired:

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

What am I doing wrong?

When you add items to the List , the ObservableCollection is not changed and thus does not fire the CollectionChanged event.

To get the event fired, you have to add the item to the ObservableCollection :

markersObservable.Add(hScrollBarPoints.Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM