Toggling buttons in a ToggleButtonBar control in Flex

by Peter deHaan on January 21, 2009

in ToggleButtonBar

The following example shows how you can toggle buttons in a Flex ToggleButtonBar control by setting the Boolean toggleOnClick property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/21/toggling-buttons-in-a-togglebuttonbar-control-in-flex/ -->
<mx:Application name="ToggleButtonBar_toggleOnClick_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="toggleOnClick:">
                <mx:CheckBox id="checkBox" />
            </mx:FormItem>
            <mx:FormItem label="selectedIndex:">
                <mx:Label text="{toggleButtonBar.selectedIndex}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ToggleButtonBar id="toggleButtonBar"
            toggleOnClick="{checkBox.selected}"
            dataProvider="{viewStack}" />

    <mx:ViewStack id="viewStack"
            width="100%"
            height="100%">
        <mx:VBox label="Red" backgroundColor="red" />
        <mx:VBox label="Orange" backgroundColor="haloOrange" />
        <mx:VBox label="Yellow" backgroundColor="yellow" />
        <mx:VBox label="Green" backgroundColor="haloGreen" />
        <mx:VBox label="Blue" backgroundColor="haloBlue" />
    </mx:ViewStack>

    <mx:Label text="{Capabilities.version}" />

</mx:Application>

View source is enabled in the following example.

{ 3 comments… read them below or add one }

1 Eric Belair January 30, 2009 at 6:16 am

I’m not sure that I see any difference between when the checkBox is selected or not. What should I be seeing?

Reply

2 Peter deHaan January 30, 2009 at 5:53 pm

Wow, I really should go back and add SWFs to these older posts.

Eric,
When the toggleOnClick CheckBox is deselected, clicking an already selected button has no effect and the selectedIndex doesn’t change. When the toggleOnClick CheckBox is selected, clicking an already selected button sets the selectedIndex property to -1.

I’ll try and get that SWF up right now…

Peter

Reply

3 mourya December 24, 2009 at 6:57 am

even when i use selectedIndex=”-1″, the first element is selected

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: