The context is that I want to expose to a ListView
in the View a ListCollectionView
from the ViewModel; that ViewModel updates slowly an ObservableCollection
(it takes few seconds to fill the collection).
Hence, I want to update a ListCollectionView
after an ObservableCollection
was updated this way:
MyObservableCollection.CollectionChanged += CollectionChanged;
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
MyListCollectionView.AddNewItem(e.NewItems);
MyListCollectionView.CommitNew();
}
I tried to update a ListCollectionView
, but it fails even with a List<>
. How can I do this ?
[TestClass]
public class ListViewTests
{
private ListCollectionView _sut;
[TestInitialize]
public void Setup()
{
var toadd = new List<int> {};
_sut = new ListCollectionView(toadd);
}
[TestMethod]
public void AddItem()
{
var toadd = new List<int> { 1,2,3 };
_sut.AddNewItem(toadd);
_sut.CommitNew();
}
}
System.InvalidOperationException: 'AddNewItem' is not allowed for this view.
For more details, how I update the ObservableCollection
:
Parallel.ForEach(ShTiffFiles, file =>
{
var sht = new ShutterTiff(file, _aesService); // slow (small Model)
var shtv = new ShutterTiffVignette(sht, _fastCache); // slow (small ViewModel)
lock (_o) // make it thread safe
{
Application.Current.Dispatcher.Invoke(() =>
{
ShutterTiffObservableCollection.Add(shtv);
});
}
});
in the ViewModel:
private object _o = new object();