开发者

Error populating ObservableCollection

开发者 https://www.devze.com 2023-03-04 03:48 出处:网络
I have this code: ObservableCollection<GuiQuestionCluster> clusters = new ObservableCollection<GuiQuestionCluster>(this.ExaminationViewModel.Examination.QuestionClusters);

I have this code:

ObservableCollection<GuiQuestionCluster> clusters = new ObservableCollection<GuiQuestionCluster>(this.ExaminationViewModel.Examination.QuestionClusters);

So, th开发者_运维技巧is.ExaminationViewModel.Examination.QuestionClusters are of the type of QuestionCluster. QuestionCLuster is the superclass of GuiQuestionCluster. The code I wrote didn't work, it gives errors. But I want to know how I fix this, is there a little trick for it?

First error:

Error 1 The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection.ObservableCollection(System.Collections.Generic.IEnumerable)' has some invalid arguments

Second:

Error 2 Argument 1: cannot convert from 'System.Collections.ObjectModel.ReadOnlyCollection' to 'System.Collections.Generic.IEnumerable'


There is no trick to this, because of the type difference. Here are your options:

  • Make it a QuestionClusters collection rather than GuiQuestionClusters
  • Use a foreach loop to add item to the list. You will have to create the object and assign value directly or use an overloaded constructor that takes the base class type. This is in principle not the best design as it can cause partial assignment.
  • You can explore using inheritance or extension method to workaround this
0

精彩评论

暂无评论...
验证码 换一张
取 消