akgulebubekir / Maui.DataGrid

DataGrid component for Maui
MIT License
261 stars 54 forks source link

Exception when adding items from another thread #185

Open daniol opened 5 months ago

daniol commented 5 months ago

Describe the bug MAUI.DataGrid does not internally handle thread synchronization for updates to its data source. When an item is added to the ObservableCollection from a thread different than the UI thread, the DataGrid does not automatically marshal the change back to the UI thread. Calling MainThread.BeginInvokeOnMainThread when adding a item to the ItemSource collection solves the problem.

To Reproduce Steps to reproduce the behavior:

  1. Define a DataGrid with a ItemsSourceassociated to an ObservableCollection
  2. Add an Item to the ObservableCollection from another thread, for example with Task.Run(() => itemList.Add(x));
  3. A COM exception is thrown when _paginationStepper.IsEnabled is changed https://github.com/akgulebubekir/Maui.DataGrid/blob/9b24fb19bcb71b05179960ef3d30dedfd2d2f069/Maui.DataGrid/DataGrid.xaml.cs#L1231

Expected behavior Either no exception, or explicitly state on the documentation that calling BeginInvokeOnMainThread is necessary.