Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple databinding issue

Simple databinding issue

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lustuyck
    wrote on last edited by
    #1

    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!

    H 1 Reply Last reply
    0
    • L lustuyck

      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!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 the IList interface, which always returns and accepts the Object type. An object must be present in order to discover the actual Type information.

      Microsoft MVP, Visual C# My Articles

      L 1 Reply Last reply
      0
      • H Heath Stewart

        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 the IList interface, which always returns and accepts the Object type. An object must be present in order to discover the actual Type information.

        Microsoft MVP, Visual C# My Articles

        L Offline
        L Offline
        lustuyck
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • L lustuyck

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Yes, but it's not simple. You need to extend the CurrencyManager class and override GetItemProperties 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 the BindingContext property, return an instance of a derivative BindingContext that you must create. In your derivative class, override Add and when the BindingManagerBase is of type CurrencyManager, add your CurrencyManager derivative by calling base.Add with your type (which should copy information from the BindingManagerBase if available). This should work, though it's untested.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups