[英]How to get ObservableCollection from Dictionary Values
I have an ObservableCollection<MyClass> myCollection
and a Dictionary<int, MyClass> myDictionary
. 我有一个ObservableCollection<MyClass> myCollection
和一个Dictionary<int, MyClass> myDictionary
。
How would I copy all the Values of Class MyClass
from the Dictionary
to the ObservableCollection
? 如何将MyClass
类的所有值从Dictionary
复制到ObservableCollection
? Unfortunately this won't work (Cannot implicitly convert type...): 不幸的是,这将不起作用(无法隐式转换类型...):
myDictionary = myCollection.Values
And I was hoping for a solution that would perform better than: 我希望能有一个比以下更好的解决方案:
for (int i = 0; i < myDicitonary.Values.Count; i++ )
myCollection.Add(myDicationary.Values.ElementAt(i));
The observable collection can be created from an IEnumerable. 可以从IEnumerable创建可观察的集合。
myCollection = new ObservableCollection<MyClass>(myDictionary.Values)
If you already have the collection created and don't want to create a new one, you'd have to iterate trough myDictionary.Values and manually add each value in the observable collection. 如果您已经创建了集合,并且不想创建一个新集合,则必须遍历myDictionary.Values并手动将每个值添加到可观察的集合中。 Note that this will trigger the collection changed event after each individual add. 请注意,这将在每个单独添加后触发集合更改事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.