首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF与vici冷库CSList的数据绑定会导致转换错误

WPF与vici冷库CSList的数据绑定会导致转换错误
EN

Stack Overflow用户
提问于 2013-07-25 03:02:22
回答 1查看 289关注 0票数 2

当我尝试使用CSList作为WPF DataGrid的ItemsSource时,会得到一个错误。

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

我不完全确定是否可以使用它作为绑定源,但是根据几乎不存在的用于vici冷库的文档,它们的集合应该适合绑定。下面是它似乎失败的那条线:

代码语言:javascript
复制
dataGrid1.ItemsSource = Product.List();

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

谢谢!

编辑这里是堆栈跟踪(稍微修改一下)

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

发布于 2013-08-05 21:35:08

虽然这不是问题的解决办法,但它是一个解决办法。根据一篇文章,这里

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

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

而不是

代码语言:javascript
复制
TestCollection c = new TestCollection();
c.Add(new Test());
comboBox1.DataSource = c;
comboBox1.DisplayMember = "Text";

他用

代码语言:javascript
复制
TestCollection c = new TestCollection();
c.Add(new Test());
Test[] arr = new Test[c.Count];
c.CopyTo(arr);
comboBox1.DataSource = arr;
comboBox1.DisplayMember = "Text";

我很想找到一个不需要乱七八糟的变通办法的解决方案,但与此同时,这至少让我克服了这个问题(不过,这有点破坏了ORM的一些功能.)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17848306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档