Home > Article > Backend Development > How to Safely Modify ObservableCollections from Background Threads in WPF?
When updating an ObservableCollection bound to a DataGrid from a thread other than the UI thread, an exception can occur: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread."
ObservableCollections are created on the UI thread. As a result, they have an affinity for that thread, meaning changes can only be made from the same thread. Attempting to modify them from another thread (e.g., a background thread) will trigger the exception.
To resolve the issue, invoke the UI Dispatcher when updating the ObservableCollection from a different thread. This will delegate the operation to the UI thread, where it can be safely executed.
public void Load() { matchList = new List<GetMatchDetailsDC>(); matchList = proxy.GetMatch().ToList(); foreach (EfesBet.DataContract.GetMatchDetailsDC match in matchList) { // This syntax invokes the UI dispatcher // and modifies the ObservableCollection on the UI thread App.Current.Dispatcher.Invoke((Action)delegate { _matchObsCollection.Add(match); }); } }
To bind a DataGrid asynchronously and refresh it when necessary:
// Bind DataGrid to ObservableCollection DataGrid.SetBinding(ItemsSourceProperty, new Binding("MatchObsCollection")); // Subscribe to CollectionChanged event MatchObsCollection.CollectionChanged += (s, e) => { DataGrid.Items.Refresh(); };
By following these guidelines, you can safely update ObservableCollections and bind DataGrids asynchronously while ensuring that changes are made on the correct thread.
The above is the detailed content of How to Safely Modify ObservableCollections from Background Threads in WPF?. For more information, please follow other related articles on the PHP Chinese website!