Hello,
I have a thread-safe observable collection. It works fine for wpf DataGrid but when I update it to XamGrid - my application freezes on validation. The collection is something similar to https://www.codeproject.com/Tips/998619/Thread-Safe-ObservableCollection-T .
<ig:XamGrid ItemsSource={"Binding Collection"} />
Thank you
Hello Ivan,
I recommend using the XamDataGrid over the XamGrid as background operations on the datasource are not supported in the XamGrid. If you must use the XamGrid then you will need to marshal all of your collection modifying operations to the UI thread using Dispatcher.BeginInvoke().
The XamDataGrid, on the other hand, has support for this using a normal ObservableCollection so there is no need for custom collection types. In .NET 4.5 Microsoft added functionality for updating bound lists from background threads using the BindingOperations.EnableCollectionSynchronization method. This works with the XamDataGrid as long as you provide the list in an ICollectionView. After this setup you can update the collection from a different thread. Here is a small snippet of how it would look:
public class ViewModel { /// <summary> /// This is what the XamDataGrid will bind to /// </summary> public ICollectionView DataView { get; set; } object _lock; ObservableCollection<MyItem> _data; public ViewModel() { _lock = new object(); _data = GetData(); BindingOperations.EnableCollectionSynchronization(_data, _lock); DataView = CollectionViewSource.GetDefaultView(_data); } private void DoBackgroundWork() { Task.Run(() => { lock(_lock) { // modify the collection _data.Add(new MyItem()); _data.RemoveAt(0); } }); } private ObservableCollection<MyItem> GetData() { ... } }
Assuming that the above ViewModel is the DataContext of your XamDataGrid, the XAML for it would be:
<igDP:XamDataGrid DataSource="{Binding Path=DataView}"/>
Thanks!