views:

230

answers:

2

Hi folks,

I have a Dictionary and want to display the number of items in this dictionary with a Label, without updating the label manually each time I add or remove an item from the dictionary.

I tried it with the Binding class:

Binding bindingNodeCount = new Binding("Text", _graphDisplay.data.nodes.Keys, "Count");
labelNumberOfNodes.DataBindings.Add(bindingNodeCount);

Unfortunately, it is not possible to bind the "Count"-Property (at least it throws me an argument exception, saying he can't bind the Property Count to the DataSource).

Is there an other method to automatically update the display of the Item-Count of my Dictionary?

Thanks in advance, Frank

+2  A: 

This isn't going to work, on multiple levels:

  • you can't bind to properties of containers (such as Keys) - it assumes you want the first item from the container (i.e. .Keys[0].Count)
  • dictionary doesn't provide notification events

To do what you want, you would probably have to have your own dictionary implementation (or subclass) with notofication events, and manually propagate that a higher-level object (data or graphDisplay) as a facade property, and forward the event.

It will be easier to just update it manually, I suspect.

Marc Gravell
Agreed, seems like a long way for a shortcut.
James
Yap, I solved it by firing an event, when the dictionary is changed and consume this event in my form.
Aaginor
+2  A: 

Not an exact duplicate, but as close as it will get: How to bind a list count to a label in WinForms

James