3 Replies Latest reply on Feb 2, 2011 7:45 AM by JoshBeall

    Is it possible to use two-way databinding with a RadioButtonGroup?

    JoshBeall Level 1

      Hi All,

       

      I've got a group of spark RadioButton controls, all of which have a "value" property that has been assigned an object of type "Membership" (that is a custom model class in my application).

       

      Now, I've also got an object called "selectedMembership" that I want to hold the currently selected Membership object, based on what RadioButton has been enabled (clicked and made the selected RadioButton), or if another piece of code has updated the value of selectedMembership I want the RadioButton that's enabled to change correspondingly.  I tried do to that like this:

       

      <s:RadioButtonGroup id="membershipTypeSelection" selectedValue="@{selectedMembership}" />

       

      I thought that I could use Flex 4's two-way databinding would do just what I needed.  Set "selectedMembership" and then the selected RadioButton should change.  Click on a different RadioButton, and the value of selectedMembership should be updated with the new Membership object.

       

      But, instead I get a compile error.  It says:

       

      Multiple markers at this line:

      -RadioButtonGroup

      -1118: Implicit coercion of a value with static type Object to a possibly unrelated type model:Membership.

       

      If I get rid of the @ (making the databinding one-way--meaning, selectedValue will always be set to selectedMembership (and if selectedMembership is every changed, selectedValue will be updated with the new value of selectedMembership); however, selectedMembership will not be updated when the user clicks on a different RadioButton, thus changing the value of selectedValue.

       

      Can I not use two-way databinding with a RadioButtonGroup because the type of selectedValue is simply "Object" and I want to bind it to a strongly typed model class?  If not, what are the workarounds/alternatives?

       

      Thanks!

        -Josh

        • 1. Re: Is it possible to use two-way databinding with a RadioButtonGroup?
          Flex harUI Adobe Employee

          Try:

           

          <s:RadioButtonGroup id="membershipTypeSelectio)n"

          selectedValue="@{Membership(selectedMembership}" /

          • 2. Re: Is it possible to use two-way databinding with a RadioButtonGroup?
            JoshBeall Level 1

            Interesting thought!  I tried that but I get the error: 1105: Target of assignment must be a reference value.

             

            I also tried:

            <s:RadioButtonGroup id="membershipTypeSelection" selectedValue="@{selectedMembership as Object}" />

             

            But I get the error: 1050: Cannot assign to a non-reference value.

             

            I was thinking it through and I think I actually want to cast to Object, not to Membership--because selectedValue is of type Object, and I want the types to match.

             

            So, I tried both methods of casting above, and got the same error.  It looks like it won't let me bind if I cast the value I'm trying to bind on.

            • 3. Re: Is it possible to use two-way databinding with a RadioButtonGroup?
              JoshBeall Level 1

              Ok, here's what I did (and it's working).

               

              I created a property with get/set methods like this:

               

               

              [Bindable]
              private function get selectedMembershipAsObject():Object{
                  var result:Object = this.selectedMembership as Object;
                  return result;
              }
              
              private function set selectedMembershipAsObject(value:Object):void{
                  var typedValue:Membership = value as Membership;
                  this.selectedMembership = typedValue;
              }
              

               

               

              Then, I setup my RadioButtonGroup like this:

               

               

              <s:RadioButtonGroup id="membershipTypeSelection"
                          selectedValue="@{selectedMembershipAsObject}" />
              

               

               

              This caused the compile errors to go away, and binding seems to be working just fine.

               

              Any reason this is a bad idea?