You could try to use bindingOperations.enableCollectionSyNChronization Method
.
The documentation is pretty clear on this:
To use a collection on multiple threads, one of which is the UI thread that owns the ItemsControl
, an application has the following responsibilities:
- Choose a synchronization mechanism.
- Synchronize all access from the application to the collection using that mechanism.
- Call
EnableCollectionSynchronization
to inform WPF of the mechanism.
- ...
You can 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 another thread
Items.Add(new Item());
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.