locked
Binding to a custom property instead of a control RRS feed

  • Question

  • Sometimes I need to bind one of the properties from a data source to a control that does not support data binding. Previously I used to do this by adding a hidden control that I could bind to, and then handling the appropriate "changed" event from that control to set the desired property on my non-bindable control.

     

    It would obviously be cleaner to do this with a custom property on my form (instead of the hidden control), but I can only get this to work one way. i.e. the data source is setting my property which is reflected in my control. But if I make changes to the control, it sets the custom property, but this is not reflected back in the data source.

     

    Here’s some sample code:

     

      ' Add my custom property to the form's data bindings

      Private Sub MyForm_Load(...) Handles Me.Load

        Me.DataBindings.Add("CostType", BindingSource, "CostType")

      End Sub

     

      ' My custom property

      Private mCostType As Integer

      Public Property CostType() As Integer

        Get

          Return mCostType

        End Get

        Set(ByVal value As Integer)

          If mCostType <> value Then

            MyControl.SelectedPageIndex = Index

            mCostType = value

          End If

        End Set

      End Property

     

      Private Sub MyControl_SelectedPageChanged(...) Handles MyControl.SelectedPageChanged

        ' The user has changed the control so reflect this in the custom property

        Me.CostType = MyControl.SelectedPageIndex

      End Sub

     

    Am I missing something obvious, or is this not simple to do.

    Friday, September 1, 2006 11:16 AM

All replies

  • Hi,

    would it help if you mark your property as bindable (by adding appropriate attribute)?

    <System.ComponentModel.Bindable(True)> _
    Public Property CostType() As Integer
        Get
           
    Return mCostType
       
    End Get
       
    ...
    End Property

    Andrej

    Friday, September 1, 2006 11:40 AM
  • No, that doesn't seem to make any difference.

    David

    Friday, September 1, 2006 12:40 PM
  • Another thing... in SelectedPageChanged handler, why are you setting your field through the property? Try setting local variable only:

    Private Sub MyControl_SelectedPageChanged(...) Handles MyControl.SelectedPageChanged

        ' The user has changed the control so reflect this in the custom property

        mCostType = MyControl.SelectedPageIndex

    End Sub

    Andrej

    Friday, September 1, 2006 1:28 PM