views:

63

answers:

1

Hi,

Working in Flex 3, I have a series of components being rendered on a canvas, each of which should represent a single potential selection, ideally in a RadioButtonGroup. So in my parent canvas I am defining the RadioButtonGroup, and each component provides a single RadioButton. However, this doesn't seem to work.

Suppose there is a component called aComponent defined as such:

<mx:Canvas ...>
 ...
 <mx:RadioButton id="someButton" groupName="myRadioButtonGroup" ... />
</mx:Canvas>

The outer canvas:

<mx:Canvas id="outerCanvas" ...>
 ...
 <mx:Script>
   public function doesSomething():void
   {
     var myComponent:aComponent = new aComponent();
     outerCanvas.addChild(myComponent);    
   }
 </mx:Script>
 ...
 <mx:RadioButtonGroup id="myRadioButtonGroup" />
</mx:Canvas>

So my guess was that at this point if, say, four of these components were added, the radio buttons would behave in mutually exclusive fashion and I'd be able to access myRadioButtonGroup.selectedValue to get the current selection. However, it doesn't seem to work that way.

Is what I'm trying to do even possible, or have I maybe just missed something?

Thanks!

A: 

Edit - got to the bottom of it:

The radiobuttongroup isn't available to the component. It's parent has 'myRadioButtonGroup', but not the component.. Pass the 'myRadioButtonGroup' to the constructor and use it..

outerCanvas function:

var myComponent:aComponent = new aComponent(myRadioButtonGroup);

aComponent definition:

private var radioGroup:RadioButtonGroup;
function aComponent(radioGroup:RadioButtonGroup):void {
  this.radioGroup = radioGroup;
}
</mx:Script>
<mx:RadioButton id="someButton" groupName="radioGroup" ... />

untested, but hopefully gives you the idea

Dan Heberden
The component actually has more to it than that, it consists of an image, text, some stylistic behavior, and holds a reference to another object. The overall goal is to make a screen where the user can select one of a number of these and I was hoping to manage the selection process using RadioButtons if it's possible. I did not think about extending RadioButtonGroup though, I am not sure I see how that would work.
futureal
Well what you want is totally posible in a canvas element, it's just your radiobuttongroup will have to be in that element.. so canvas > radiobuttongroup > radiobuttons. you can write any logic you want in the component and still access that components radiobuttongroup.
Dan Heberden
Yea, that is exactly what I thought as well (and how I have it laid out above in pseudo-code, at least I think). Unfortunately it is not working. It's possible I missed something.
futureal
you know, it in a canvas still might work. your issue is scope. In the component, there _is_ no myRadioButtonGroup - that's part of mx.core.application.Application (don't remember the new flexglobals one off the top of my head). You could pass myRadioButtonGroup to the component in your constructor.
Dan Heberden
Updated the answer
Dan Heberden