2

当我尝试将 CSList 用作 WPF DataGrid 的 ItemsSource 时,出现错误

无法将“System.Object[]”类型的对象转换为“Product[]”类型

我不完全确定是否可以将其用作绑定源,但根据几乎不存在的 vici coolstorage 文档,他们的集合应该适合绑定。这是它似乎失败的行:

dataGrid1.ItemsSource = Product.List();

有人可以告诉我这是否可能,如果是,我做错了什么?

谢谢!

[编辑] 这是堆栈跟踪(略有改动)

  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
4

1 回答 1

1

虽然这不是问题的解决方案,但它是一种解决方法。根据这里的帖子:

应该接受 Object[] 数组但不接受的 ICollection.CopyTo 方法,因为它们想要调用强类型的 CopyTo 方法

该论坛中的 OP 通过绑定到他的类型化集合的显式创建的数组副本来解决该问题。

代替

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

他曾经

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

我很想有一个不需要笨拙的解决方法的解决方案,但与此同时,这至少让我克服了问题(虽然有点破坏了 ORM 的一些功能......)

于 2013-08-05T21:35:08.457 回答