开发者

Simple databinding - How to handle bound field/property change. Winforms, .Net

开发者 https://www.devze.com 2022-12-08 17:39 出处:网络
I have a custom control with a bindable property:- Private _Value As Object <Bindable(True), ... > _

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
        开发者_Go百科    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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消