Simple data binding. How to handle changing a bound field / property. Winforms, .Net
I have a custom control with 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
Anytime the field whose value is bound changes, I need to get the type.
I do this in two places. First, 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 am adding a handler for the DataBindings.CollectionChanged event. This is the second place where I extract 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 doesn't fire until some time after InitializeComponent. The second place is required if the binding field is programmatically changed.
Am I handling the correct events here, or is there a cleaner way to do this?
Note. The GetValueType method uses the CurrencyManager.GetItemProperties .... etc function to get the type.
Greetings,
Jules
ETA: To be clear here, I want to know when the associated field changed, not the value of the associated field.
source to share
It looks like you are looking for an INotifyPropertyChange interface that automatically notifies related update controls.
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
source to share