Simple databinding issue
-
Hi, I have a ClassA (extends Component) with some properties. I have a ClassB (extends CollectionBase) that implements collection of ClassA objects. In my main app, I use a ClassB collection, add some ClassA objects to it, and databind a property X of ClassA to a textbox: textBox.DataBindings.Add("Text", ClassBCcollection, "PropertyX"); This works well, changes in the textbox are reflected in the collection. EXCEPT when at startup the ClassB collection is empty, then databinding does not work. So; databinding works when databindings are set after collection is filled with items; databinding does not work when databindings are set when collection is empty. Do I miss something...? Thanks!
-
Hi, I have a ClassA (extends Component) with some properties. I have a ClassB (extends CollectionBase) that implements collection of ClassA objects. In my main app, I use a ClassB collection, add some ClassA objects to it, and databind a property X of ClassA to a textbox: textBox.DataBindings.Add("Text", ClassBCcollection, "PropertyX"); This works well, changes in the textbox are reflected in the collection. EXCEPT when at startup the ClassB collection is empty, then databinding does not work. So; databinding works when databindings are set after collection is filled with items; databinding does not work when databindings are set when collection is empty. Do I miss something...? Thanks!
lustuyck wrote: Do I miss something...? Nope. The reason is you read deep into the data-binding topics is because when you use a
CurrencyManager
to bind against a collection of unknown object types, the collection or list must contain all the same type (or base types that must come before their derivative types) and contain at least one of those types for the data-binding mechanism to discover the Type information correctly. Even if you use typed parameters and return values, this still won't help since it uses theIList
interface, which always returns and accepts theObject
type. An object must be present in order to discover the actual Type information.Microsoft MVP, Visual C# My Articles
-
lustuyck wrote: Do I miss something...? Nope. The reason is you read deep into the data-binding topics is because when you use a
CurrencyManager
to bind against a collection of unknown object types, the collection or list must contain all the same type (or base types that must come before their derivative types) and contain at least one of those types for the data-binding mechanism to discover the Type information correctly. Even if you use typed parameters and return values, this still won't help since it uses theIList
interface, which always returns and accepts theObject
type. An object must be present in order to discover the actual Type information.Microsoft MVP, Visual C# My Articles
Hi, thanks for the answer. What I do now as a 'workaround', is wait until an item is added to the collection the first time; and when that happens I add the databindings (only once). It works now, but I don't know if this is common practice. Are there better ways to do something like this? Kind regard, Ludwig
-
Hi, thanks for the answer. What I do now as a 'workaround', is wait until an item is added to the collection the first time; and when that happens I add the databindings (only once). It works now, but I don't know if this is common practice. Are there better ways to do something like this? Kind regard, Ludwig
Yes, but it's not simple. You need to extend the
CurrencyManager
class and overrideGetItemProperties
to return the properties of the Type. You can do this simply by returning something like the following:public override PropertyDescriptorCollection GetItemProperties()
{
return TypeDescriptor.GetProperties(typeof(MyClass), new Attribute[] {
new BindableAttribute(true) });
}Then, extend the
Control
classes that you want to data-bind and override theBindingContext
property, return an instance of a derivativeBindingContext
that you must create. In your derivative class, overrideAdd
and when theBindingManagerBase
is of typeCurrencyManager
, add yourCurrencyManager
derivative by callingbase.Add
with your type (which should copy information from theBindingManagerBase
if available). This should work, though it's untested.Microsoft MVP, Visual C# My Articles