Is there a Threadsafe Observable collection in .NET 4?

There are two possible approaches. The first would be to inherit from a concurrent collection and add INotifyCollectionChanged functionality, and the second would be to inherit from a collection that implements INotifyCollectionChanged and add concurrency support. I think it is far easier and safer to add INotifyCollectionChanged support to a concurrent collection. My suggestion is … Read more

ObservableCollection not updating View

You need to raise the change notification on the Level2MenuItems property. Instead of having public ObservableCollection<EMSMenuItem> Level2MenuItems { get; set; } you need private ObservableCollection<EMSMenuItem> _level2MenuItems; public ObservableCollection<EMSMenuItem> Level2MenuItems { get { return _level2MenuItems; } set { _level2MenuItems = value; RaisePropertyChanged(nameof(Level2MenuItems)); } } The reason the former works in the constructor is that the Binding … Read more

ObservableCollection and Item PropertyChanged

Here is how you would attach/detach to each item’s PropertyChanged event. ObservableCollection<INotifyPropertyChanged> items = new ObservableCollection<INotifyPropertyChanged>(); items.CollectionChanged += items_CollectionChanged; static void items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (INotifyPropertyChanged item in e.OldItems) item.PropertyChanged -= item_PropertyChanged; } if (e.NewItems != null) { foreach (INotifyPropertyChanged item in e.NewItems) item.PropertyChanged += item_PropertyChanged; } } … Read more

How to Avoid Firing ObservableCollection.CollectionChanged Multiple Times When Replacing All Elements Or Adding a Collection of Elements

ColinE is right with all his informations. I only want to add my subclass of ObservableCollection that I use for this specific case. public class SmartCollection<T> : ObservableCollection<T> { public SmartCollection() : base() { } public SmartCollection(IEnumerable<T> collection) : base(collection) { } public SmartCollection(List<T> list) : base(list) { } public void AddRange(IEnumerable<T> range) { foreach … Read more

Using BindingOperations.EnableCollectionSynchronization

All the examples I’ve seen on Stack Overflow for this get it wrong. You must lock the collection when modifying it from another thread. On dispatcher (UI) thread: _itemsLock = new object(); Items = new ObservableCollection<Item>(); BindingOperations.EnableCollectionSynchronization(Items, _itemsLock); Then from another thread: lock (_itemsLock) { // Once locked, you can manipulate the collection safely from … Read more

How to make ObservableCollection thread-safe?

As of .net framwork 4.5 you can use native collection synchronization. BindingOperations.EnableCollectionSynchronization(YourCollection, YourLockObject); YourLockObject is instance of any object e.g. new Object();. Use one per collection. This eliminates the need of some special class or anything. Just enable and enjoy 😉 [edit] As stated in the comments by Mark and Ed (thanks for clarifying!), this … Read more

WPF MVVM INotifyPropertyChanged Implementation – Model or ViewModel

The thing is that if you were following MVVM, you would have a BookViewModel for your Book model class. So you would have a INotifyPropertyChanged implementation on that view model. Exactly for that purpose MVVM exists (but not only). That being said, the INotifyPropertyChanged has to be implemented on view model classes, not models. UPDATE: … Read more

ObservableCollection and threading [duplicate]

The best way to solve this is to pass the Dispatcher object to the start method of the background thread. void DoBackgroundOperation(ObservableCollection<SomeType> col) { var dispatcher = Dispatcher.CurrentDispatcher; ThreadStart start = () => BackgroundStart(dispatcher, col); var t = new Thread(start); t.Start(); } private static void BackgroundStart( Dispatcher dispatcher, ObservableCollection<SomeType> col) { … SomeType t = … Read more