views:

394

answers:

1

I have a custom control with a bindable property:-

Private _Value As Object
<Bindable(True), ... > _
Public Property Value() As Object
    Get
        Return _Value
    End Get
    Set(ByVal value As Object)
        _Value = value
    End Set
End Property

Any time the field, that Value is bound to, changes, I need to get the type.

I do this at two places. Firstly at OnBindingContextChanged:-

Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
    MyBase.OnBindingContextChanged(e)
    RemoveHandler Me.DataBindings.CollectionChanged, AddressOf DataBindings_CollectionChanged
    AddHandler Me.DataBindings.CollectionChanged, AddressOf DataBindings_CollectionChanged
    Me.MyBinding = Me.DataBindings("Value")
    If Me.MyBinding IsNot Nothing Then
        Me.GetValueType(Me.MyBinding)
    End If
End Sub

Also, here, I'm adding a handler to the DataBindings.CollectionChanged event. This is the second place that I retrieve the type:-

Private Sub DataBindings_CollectionChanged(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)

    If e.Action = CollectionChangeAction.Add Then

        Dim b As Binding = DirectCast(e.Element, Binding)
        If b.PropertyName = "Value" Then
            Me.GetValueType(b)
        End If

    End If
End Sub

I need the first place, because the BindingContextChanged event is not fired until some time after InitializeComponent. The second place is needed if the binding field is programatically changed.

Am I handling the correct events here, or is there a cleaner way to do it?

Note: My GetValueType method uses the CurrencyManager.GetItemProperties....etc, to retrieve the type.

Cheers,

Jules

ETA: Just to be clear here, I want to know when the bound field has changed, not the bound field value.

A: 

It sounds like you are looking for the INotifyPropertyChange interface, which will automatically notify bound controls of an update.

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Charlie Brown
Hi, I don't think that's what I'm looking for. I want to know when the bound field changes, not the bound field value. For instance My control's Value property could be bound to a source "Name" field. If this is changed to "Address" field, then I want to know. I can't see why this would happen, but it's important that I code for it, just in case.
Jules
Actually, that was a bad example above. A better example would be if the bound field was changed from "Name" to "FavouriteColour", so I could change the type.
Jules