WinForm binding combo box

WinForm binding combo box

Old forum URL: forums.lhotka.net/forums/t/10218.aspx


sjsmith posted on Tuesday, March 29, 2011

I'm trying to implement a solution using the CSLA 4 framework with a WinForm interface.  I have a BusinessBase class that represents an address.  The address class has an editable property for State and County, and read-only lists for States and Counties.  The goal is to have the class refresh the list of counties each time the State property changes.  This way the CountyList contains a list of counties for the current state.  Using the code below, I'm able to make the class rebuild the CountyList each time the State property changes.

Public Shared ReadOnly StateProperty As PropertyInfo(Of String) = RegisterProperty(Of String)(Function(c) c.State, "State")
    Public Property State() As String
        Get
            Return GetProperty(StateProperty)
        End Get
        Set(ByVal value As String)
            SetProperty(StateProperty, value)

            SetProperty(CountyProperty, vbNullString)
            SetProperty(CountyListProperty, CountyLst.GetList(Me.DatabaseID, Me.State))

        End Set
    End Property

I'm new to the data binding framework and can not figure out how to have the address class notify the WinForm interface that it needs to rebuild (or refresh) the data source of the CountyListBindingSource. Setting the CountyListProperty in the code above, does not trigger an event that I can capture on the interface.  It would seem that I'm missing something simple (method or event) that is available to me to make this happen.  Any suggestions for a newbie?  Thanks.

ajj3085 replied on Tuesday, March 29, 2011

Don't call the SetProperty for your Country or CountryList propery in the setter of the State.  Instead, create a business rule which does those functions; Country and CountryList would then be listed as AffectedProperties, which I believe should cause the refresh.

sjsmith replied on Wednesday, March 30, 2011

My attempt to implement a business rule to reset the county properties was not successful.  I added a business rule for the StateProperty that accepts the county name and county list properties.  The rule is changing the county name property to vbnullstring and reloading the correct list of county names,  Furthermore, the text property for the county combo box is changing to nullstring, but once again the interface is not updating the list of values that are bound to the county combo box.  This is the business rule class.

    <Serializable()>
    Private Class ResetCounty
        Inherits BusinessRule

        Dim _countyProperty As Core.IPropertyInfo
        Dim _countyListProperty As Core.IPropertyInfo
        Dim _databaseProperty As Core.IPropertyInfo

        Public Sub New(ByVal stateProperty As Core.IPropertyInfo,
                       ByVal countyProperty As Core.IPropertyInfo,
                       ByVal countyListProperty As Core.IPropertyInfo,
                       ByVal databaseProperty As Core.IPropertyInfo)

            MyBase.New(stateProperty)

            _countyProperty = countyProperty
            _countyListProperty = countyListProperty
            _databaseProperty = databaseProperty

            InputProperties = New List(Of Csla.Core.IPropertyInfo)

            InputProperties.Add(stateProperty)
            InputProperties.Add(databaseProperty)

            AffectedProperties.Add(_countyProperty)
            AffectedProperties.Add(_countyListProperty)

        End Sub

        Protected Overrides Sub Execute(ByVal context As Csla.Rules.RuleContext)

            Dim state As String = context.InputPropertyValues(PrimaryProperty)
            Dim dbase As Enum_Database = context.InputPropertyValues(_databaseProperty)


            context.AddOutValue(_countyProperty, vbNullString)
            context.AddOutValue(_countyListProperty, CountyLst.GetList(dbase, state))

        End Sub

    End Class

Is it possible that my CountyListProperty definition is incorrect?     

Public Shared ReadOnly CountyListProperty As PropertyInfo(Of CountyLst) = RegisterProperty(Of CountyLst)(Function(c) c.CountyList, "CountyList")
    Public Property CountyList() As CountyLst
        Get
            If FieldManager.FieldExists(CountyListProperty) = False Then
                LoadProperty(CountyListProperty, CountyLst.GetList(Me.DatabaseID, Me.State))
            End If
            Return GetProperty(CountyListProperty)
        End Get
        Private Set(ByVal value As CountyLst)
            SetProperty(CountyListProperty, value)
        End Set
    End Property

Again, it appears like the list is repopulated as expected, but there is no event handled on the interface to instruct the form to update the DataSource for the combo box control.

Copyright (c) Marimer LLC