Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Observable collection thread-safe for xamGrid


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 .

<ig:XamGrid ItemsSource={"Binding Collection"} />

Thank you

  • 34510
    Suggested Answer
    Offline posted

    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(() =>
    				// modify the collection
    				_data.Add(new MyItem());
    	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}"/>