WPF Datagrid binding with vici coolstorage CSList causes cast error

300 views Asked by At

When I try to use a CSList as the ItemsSource for a WPF DataGrid, I get an error

Unable to cast object of type 'System.Object[]' to type 'Product[]'

I'm not entirely sure it is possible to use this as a binding source, but according to the almost non-existent documentation for vici coolstorage, their collections are supposed to be suitable for binding. Here's the line it appears to be failing on:

dataGrid1.ItemsSource = Product.List();

Can someone tell me if this is even possible, and if it is, what I am doing wrong?

Thanks!

[EDIT] Here's the stack trace (slightly altered)

  at Vici.CoolStorage.CSList`1.System.Collections.ICollection.CopyTo(Array array, Int32 index) in {...}\Vici.CoolStorage\Library\CSListGeneric.cs:line 1070
   at System.Collections.ArrayList.InsertRange(Int32 index, ICollection c)
   at System.Collections.ArrayList.AddRange(ICollection c)
   at System.Collections.ArrayList..ctor(ICollection c)
   at System.Windows.Data.BindingListCollectionView.RebuildListsCore()
   at System.Windows.Data.BindingListCollectionView.RebuildLists()
   at System.Windows.Data.BindingListCollectionView.<SubscribeToChanges>b__1f()
   at MS.Internal.Data.SynchronizationInfo.AccessCollection(IEnumerable collection, Action accessMethod, Boolean writeAccess)
   at System.Windows.Data.BindingOperations.AccessCollection(IEnumerable collection, Action accessMethod, Boolean writeAccess)
   at System.Windows.Data.BindingListCollectionView.SubscribeToChanges()
   at System.Windows.Data.BindingListCollectionView..ctor(IBindingList list)
   at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, DependencyObject d, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
   at {...}.ViewProduct..ctor() in {...}\ViewProduct.xaml.cs:line 37
   at {...}.MainWindow..ctor() in {...}\MainWindow.xaml.cs:line 26
1

There are 1 answers

1
Quasi_Stomach On

While this is not a solution to the problem, it is a workaround. According to a post here:

the ICollection.CopyTo methods that should accept an Object[] array but don't, because they want to call the strongly-typed CopyTo methods

The OP in that forum worked around the issue by binding to an explicitly created array copy of his typed collection.

instead of

TestCollection c = new TestCollection();
c.Add(new Test());
comboBox1.DataSource = c;
comboBox1.DisplayMember = "Text";

he used

TestCollection c = new TestCollection();
c.Add(new Test());
Test[] arr = new Test[c.Count];
c.CopyTo(arr);
comboBox1.DataSource = arr;
comboBox1.DisplayMember = "Text";

I'd love to have a solution that doesn't require kludgy workarounds, but in the meantime, this at least got me past the problem (kinda breaks some of the functionality of the ORM, though...)