Quick Flex Hack: Turn a LinkBar into a ToggleButtonBar
I love how Flex’s (talking Flex 2 here, we haven’t jumped to Flex 3 yet at the day-job) LinkBar looks, but I’m not a big fan of view stacks. I wanted to use one just as you would normally use a ToggleButtonBar, but by default the LinkBar only grays out the “selected” item if its dataProvider is actually a ViewStack. Solution: subclass it and toss in a little hack that gets the results we want. Without further adieu, here’s the code to turn a link bar into a toggle bar:
package {
import mx.controls.*;
import mx.events.*;
public class ToggleLinkBar extends LinkBar {
public function ToggleLinkBar() {
super();
addEventListener(ItemClickEvent.ITEM_CLICK,changeToggle);
}
private function changeToggle(e:ItemClickEvent):void {
for(var i:int = 0; i < numChildren; i++) {
Button(getChildAt(i)).enabled = (selectedIndex != i);
}
}
}
}
Works like a charm!






