Use ".items" on an ObservableCollection ? (loop through it)
I got a "well-constituted" ObservableCollection and I'd like to inspect into it. The definition of it is :
private Dictionary<string, ObservableCollection<DependencyObject>> _DataPools = new Dictionary<string, ObservableCollection<DependencyObject>>();
(yep it's an obsCol with an obsCol in it, but it IS ok, the problem's not here)
I tried 2 different ways but they both don't work...
1) .Items
foreach(ObservableCollection<DependencyObject> obj in _DataPools.Items)
{
blablaaaa;
....
}
.Items doesn't work but when I look in the C#doc, Items is a valid field... (just like "Count", and "Count" works...)
2) Count + [x] acces :
var nbItems = _DataPools.Count;
for (int i = 0; i < nbItems; i++)
{
Console.WriteLine("Items : " + _DataPools[i].XXX); //XXX = ranom method
}
_DataPools[i] doesn't work but on the web I found a couple of example where it is used oO
I 开发者_如何学JAVAtried a couple of other "exotic" things but I really can't go over it...
Any help will be welcomed !
Thx in advance and sry for my langage, im french -_- (sry for my president too !)
_DataPools
is a dictionary, not an ObservableCollection.
You need to loop over .Values
.
_DataPool
is not an observable collection in an observable collection. It's a dictionary that contains observable collections. To get the collections from the dictionary, use the Values
property:
foreach (ObservableCollection<DependencyObject> obj in _DataPools.Values)
{
// some code
}
_DataPools is of type Dictionary. you can access the elements of a dictionary with Values
property.
foreach(ObservableCollection<DependencyObject> obj in _DataPools.Values)
You are using a Dictionary, therefore you should use the property Keys to loop on keys and Values to loop on your items. This should work:
foreach(ObservableCollection<DependencyObject> obj in _DataPools.Values)
{
blablaaaa;
....
}
精彩评论