![]() ![]() That's it! Just use the collection as you would do with any other thread-safe list. No need to lock here as the collection is thread-safeĬlick the button and observe that everything works great! Public ConcurrentObservableCollection Items = new ConcurrentObservableCollection() Ĭreate a new ConcurrentObservableCollection in the code-behind or the view-model and bind the AsObservable property to a controlĪddItems = new DelegateCommand(AddItemsImpl).There is 2 pending events (Add) waiting for the dispatcher to be processedĮventually the dispatcher will process the pending items and do the change to the observable collection ⇒ Both collections contain the same data and the UI should be updatedĪdd the NuGet package ( NuGet, GitHub ).The bindable collection is still empty has the dispatcher hasn't run the synchronization methodĪdd a second item to the collection ⇒ The first collection contains 2 items, the bindable collection is still empty has the dispatcher hasn't run the synchronization method.There is 1 pending event (Add) waiting for the dispatcher to be processed, Dispatcher.BeginInvoke is called.Initialize the collection ⇒ Both collections are empty ![]() Here's an overview of how it work when you add items in a ConcurrentObservableCollection: While you should not manipulate the observable collection directly, some WPF controls such as the DataGrid need to add items in this list. Any access to the bind-able collection must be done from the UI thread.Any change to the collection is directly made to the thread-safe collection and will be replicated to the bind-able collection on the UI thread (using the Dispatcher).This observable collection implements INotif圜ollectionChanged, INotifyPropert圜hanged and IReadOnlyList, so it integrates well with WPF controls. You can safely bind collection.AsObservable to a WPF control.You don't need to lock to access the collection from multiple threads. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |