Guillaume Slashy
Guillaume Slashy

Reputation: 3624

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 tried 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 !)

Upvotes: 0

Views: 2174

Answers (4)

Ucodia
Ucodia

Reputation: 7710

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;
    ....
}

Upvotes: 0

tafa
tafa

Reputation: 7326

_DataPools is of type Dictionary. you can access the elements of a dictionary with Values property.

foreach(ObservableCollection<DependencyObject> obj in _DataPools.Values)

Upvotes: 0

svick
svick

Reputation: 244837

_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
}

Upvotes: 0

SLaks
SLaks

Reputation: 887469

_DataPools is a dictionary, not an ObservableCollection.
You need to loop over .Values.

Upvotes: 1

Related Questions