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

これは問題の解決策ではありませんが、回避策です。ここの投稿によると:

厳密に型指定された CopyTo メソッドを呼び出す必要があるため、Object[] 配列を受け入れる必要があるが受け入れない ICollection.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 に答える