Search code examples
data-bindingradio-buttonflash-builderflex4.5selectedvalue

DataBinding Spark RadioButtonGroup to Spark RadioButton Value


I have two radio buttons I am trying to bind to the value of a dataProvider. It populates the value initially, but if I change dp in my code it doesn't do anything. Am I missing something?

Here is a working example

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
>
    <fx:Declarations>
        <s:RadioButtonGroup id="oneGroup" selectedValue="{dataProvider.one}"/>
        <s:RadioButtonGroup id="twoGroup" selectedValue="{dataProvider.two}"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[

            import mx.collections.ArrayCollection;

            [Bindable] 
            public var dataProvider:Object = {one:"active",two:false};

            public function doinit(e:Event):void {
                dataProvider.one = dataProvider.one == "inactive" ? "active":"inactive";
                dataProvider.two = !dataProvider.two;
                result.text = dataProvider.one + ":" + dataProvider.two; 
            }
        ]]>
    </fx:Script>
    <mx:HBox>
        <mx:Form>
            <mx:FormItem label="One" direction="horizontal" >
                <s:RadioButton groupName="oneGroup" value="active" label="Active" />
                <s:RadioButton groupName="oneGroup" value="inactive" label="Inactive" />
            </mx:FormItem>
            <mx:FormItem label="Two" direction="horizontal" >
                <s:RadioButton groupName="twoGroup" value="true" label="True" />
                <s:RadioButton groupName="twoGroup" value="false" label="False" />
            </mx:FormItem>
            <s:Button label="Change DP" click="doinit(event)" />
            <s:Label id="result" />
        </mx:Form>
    </mx:HBox>
</s:Application>

Link to documentation... http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/RadioButtonGroup.html#selectedValue


Solution

  • Try to use the following class to have advantage of data binding:

    package
    {
    
    [Bindable]
    public class DP
    {
        public var one:String;
        public var two:Boolean;
    
        public function DP(one:String, two:Boolean)
        {
            this.one = one;
            this.two = two;
        }
    }
    }
    

    So for your sample:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        >
        <fx:Declarations>
            <s:RadioButtonGroup id="oneGroup" selectedValue="{dataProvider.one}"/>
            <s:RadioButtonGroup id="twoGroup" selectedValue="{dataProvider.two}"/>
        </fx:Declarations>
        <fx:Script>
        <![CDATA[
            [Bindable] 
            public var dataProvider:DP = new DP("active", false);
    
            public function doinit(e:Event):void {
                dataProvider.one = "inactive";
                dataProvider.two = true;
            }
        ]]>
        </fx:Script>
        <mx:HBox>
            <mx:Form>
                <mx:FormItem label="One" direction="horizontal" >
                    <s:RadioButton group="{oneGroup}" value="active" label="Active" />
                    <s:RadioButton group="{oneGroup}" value="inactive" label="Inactive" />
                </mx:FormItem>
                <mx:FormItem label="Two" direction="horizontal" >
                    <s:RadioButton group="{twoGroup}" value="true" label="True" />
                    <s:RadioButton group="{twoGroup}" value="false" label="False" />
                </mx:FormItem>
                <s:Button label="Change DP" click="doinit(event)" />
            </mx:Form>
        </mx:HBox>
    </s:Application>