binding to child collection.Count

binding to child collection.Count

Old forum URL: forums.lhotka.net/forums/t/196.aspx


mtagliaf posted on Wednesday, May 24, 2006

I would like a label on a form to represent the number of items in a child collection. I've tried

lb.DataBindings.Add("text", fParent.Children, "Count")
lb.DataBindings.Add("text", fParent, "Children.Count")

Neither of these work.  Do I have to create a property on the root object that mirrors the children.Count property so I can bind to this?

matt tag

xal replied on Wednesday, May 24, 2006

Matt,
There is no propertyhaschanged running there, so basically you could:
Create that property (say, ChildCount), and handle listchanged in the root object. When listchanged is fired you could do PropertyHasChanged("ChildCount").

Just a thought...

Andrés

hurcane replied on Wednesday, May 24, 2006

By default, when data binding is bound to an object that implements an interface that makes it look like a list (IList, IEnumerable, etc.), the properties are passed to the current contained object. Data binding does not allow you to bind to a property of the collection when the collection is the data source.

You can bind this way: control.Bindings.Add("Text", "MyCollection.Count", "")

This creates more problems than it solves. The primary problem that I have is this introduces a new datasource (an integer). I'm not sure that PropertyChanged() events work with the integer data source.

The other suggestion sounds like a good workaround. I thought it might be helpful to know why it didn't work.

Copyright (c) Marimer LLC