I have a ObservableCollection bound to a DataGrid. I would like to update the collection inside another class that runs a async function. Currently when I try to add it normally I get the error : This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread
. Reading other questions many suggest using BindingOperations.EnableCollectionSynchronization
but I have no idea how to implement it and I can't find any examples that run it from another class. Is this this the right way to update the collection, as they would be updated very frequently, or is there some better way?
As for the code, Main class:
public DataStore dataStore = new DataStore();
public MainWindow() {
InitializeComponent();
requestView.ItemsSource = dataStore.requestData;
responseView.ItemsSource = dataStore.responseData;
DataRetriver drt = new DataRetriver(dataStore);
}
And the class that retrives data:
public class DataRetriver
{
DataStore localStore;
public BidAskDataRetriver(DataStore ds)
{
this.localStore = ds;
runA();
}
public void runA()
{
//Build listener
listener.Bind("data", (dynamic data) => {
//data = json
parseData(data);
});
}
parseData(dynamic data)
{
//parse data and make a list with items for collection
foreach(myClass item in items)
localStore.requestData.Add(item);
}
}