Question

Consider the following radio button example.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
    private function getRb1():RadioButton {
        trace(rb1 == null);
        return rb1;                     
    }   
]]>
 </mx:Script>
<mx:VBox>
    <mx:RadioButtonGroup **id="rbg" selection="{getRb1()}**"/>      
    <mx:RadioButton id="rb1" label="Radio Button 1" />
    <mx:RadioButton id="rb2" label="Radio Button 2" />
    <mx:RadioButton id="rb3" label="Radio Button 3" />
</mx:VBox>  
 </mx:Application>

The problem is that I can not refer to rb1 while defining RadioButtonGroup, rb1 is null at that time, but i can use the selectedValue to set the initial selction.

I was just wondering is this some special case or its not safe to refer to components in mxml in general.

Thanks,

Was it helpful?

Solution

I'm not quite sure what you're asking, but hopefully this'll answer your question -- from the Flex docs:

RadioButtonGroup.selection
Contains a reference to the currently selected RadioButton control in the group. You can access the property in ActionScript only; it is not settable in MXML. Setting this property to null deselects the currently selected RadioButton control.

In general, though, making component references in MXML is totally fine; that's how effects are often handled, among many other things. For example:

<mx:Glow id="g" />
<mx:Label showEffect="{g}" />

However in your case, assuming you're having trouble setting the selected item, it might be because you haven't specified the group attribute on the radio buttons; omitting that detaches the group component from the individual buttons. Once you add that, you can bind the group's selection property using a Bindable variable containing a reference to a component, like so:

<mx:Script>
    <![CDATA[

        [Bindable]
        private var selectedRadioButton:RadioButton;    

        private function this_creationComplete(event:Event):void
        {
            selectedRadioButton = rb1;
        }

        private function btn_click(event:Event):void
        {
            selectedRadioButton = rb2;
        }

    ]]>
 </mx:Script>
<mx:VBox>
    <mx:RadioButtonGroup id="rbg" selection="{selectedRadioButton}" />
    <mx:RadioButton id="rb1" group="{rbg}" label="Radio Button 1" />
    <mx:RadioButton id="rb2" group="{rbg}" label="Radio Button 2" />
    <mx:RadioButton id="rb3" group="{rbg}" label="Radio Button 3" />

    <mx:Button label="Choose a Different Button" click="btn_click(event)" />
</mx:VBox> 

Does this make sense? Hopefully it's not completely off the mark; post back and let me know and I'll try to help out as best I can.

OTHER TIPS

Generally: just because a control was declared in MXML does not mean it is available at runtime (it might be deleted from AS, not created yet, not added to stage, therefore some properties are not available yet). This indicates it is not safe to access components at runtime and depend on values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top