Binding to Properties of a Custom Object

Basically I have an object with several properties:

Prop1,
Prop2,
etc.

I want to be able to take this object and bind its properties to various controls via the standard data-binding methodology (not like txt.Text = obj.Prop1) such that I can also use the object for 2-way data-binding.

How may I do this  Are there any interfaces I need to implement  


Answer this question

Binding to Properties of a Custom Object

  • Fatou

    What if one of the propertys is itself an object that exposes propertys. How can I handle that the same as above.

    My only sollution is to expose the propertys property as a property in the "first object", but tha doesnt’t look nice.

  • Peter Homberger

    Thanks for the reply.

    To clarify, my problem is actually with the data entity class, not with the control, where Bindable is already flagged true for the relevant properties.

    Here's the progress I've made thus far: my initial problem was that a TrackBar wasn't updating a "Height" property after having its DataBindings set. I discovered that calling myTrackBar.DataBindings["Height"].BindingManagerBase.EndCurrentEdit(); caused the TrackBar to flush its Value to the entity class, but now I've experienced a new problem...

    The "Height" property is bound to both the TrackBar AND a TextBox. When I update the TextBox, the TrackBar reflects the changes to the property, but not vice-versa; the TextBox doesn't reflect changes made with the TrackBar. If I set the "Height" property programmatically, both the TrackBar AND TextBox reflect the changes.

    I have, of course, implemented property change events in my entity class.

  • SobyOne

    use Bindable attribute on your properties.

    [Bindable(true)]
    Prop1
    {
    get{...}
    set{....}
    }

    [Bindable(true)]
    Prop2
    {
    get{...}
    set{...}
    }



  • WIreD 0x90

    I have the same binding problem. I don't understand I use a property of a type different from object it works peferctly sometimes.
  • Daniel Mesias

    'For the binding to to work the Custom class must send back notifications that a property value
    'has changed to do that code an eventhandler and raise it in the set of the property.
    'Here is an example :

    Public Class CustomClass

        Public Event IDChanged As EventHandler
        Public Event DescriptionChanged As EventHandler
        'These events will allow the databinding mechanism in .Net to receive 
        'notifications when the values change.
        'It is important that you name them exactly the same as the property name 
        '(including capitalization) and add the word "Changed" to the end with a capital C.
        'And the event must be declared as "EventHandler"


        Private m_ID As String = String.Empty 'variables must be initialized for databinding to work.
        Public Property ID() As String
            Get
                Return m_ID
            End Get
            Set(ByVal Value As String)
                m_ID = Value
                'raise the event so databinding is notified
                RaiseEvent IDChanged(Me, New EventArgs)
            End Set
        End Property

        Private m_Description As String = String.Empty 'variables must be initialized for databinding to work.
        Public Property Description() As String
            Get
                Return m_Description
            End Get
            Set(ByVal Value As String)
                m_Description = Value
                'raise the event so databinding is notified
                RaiseEvent DescriptionChanged(Me, New EventArgs)
            End Set
        End Property

    End Class

  • Binding to Properties of a Custom Object