1

I am trying to use a combobox in flex with an array to change to a canvas in a view stack. Can I do this with some custom action script? The UI I am designing could really benefit form this.

3 Answers3

3

Here's a little demo app that demonstrates what you are trying to do:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

    <mx:Script>
        <![CDATA[
            import mx.core.Container;

            private function onComboBoxChange():void
            {
                stackNav.selectedChild = this[comboNav.selectedItem];
            }            

        ]]>
    </mx:Script>

    <mx:ComboBox id="comboNav" dataProvider="{['canvas1','canvas2']}" change="onComboBoxChange()"/>

    <mx:ViewStack id="stackNav" width="400" height="300">
        <mx:Canvas id="canvas1" width="100%" height="100%">
           <mx:Label text="Hello" horizontalCenter="0" verticalCenter="0"/>
        </mx:Canvas>
        <mx:Canvas id="canvas2" width="100%" height="100%">
           <mx:Label text="World!" horizontalCenter="0" verticalCenter="0"/>
        </mx:Canvas>
    </mx:ViewStack>

</mx:Application>

You can change the logic in onComboBoxChange() to use selectedIndex as well:

stackNav.selectedIndex = comboNav.selectedIndex;
cliff.meyers
  • 17,666
  • 5
  • 51
  • 66
0

You can use this solution, but be warned that you can not pike a word with space (try to change "canvas1" to "canvas 1") and you will see:

[Bindable] private  var models:Array = ["tasks","users","bugs"];
public function changeViewStackModel():void { 
    //this.modelViewStack.selectedChild=users; //works
//this.modelViewStack.selectedChild="users"; //does not work
//this.modelViewStack.selectedChild=this.modelsCombo.selectedItem; //does not work

    switch(modelsCombo.selectedItem) {
    case "tasks": modelViewStack.selectedChild=tasks; break;
    case "users": modelViewStack.selectedChild=users; break;
    case "bugs": modelViewStack.selectedChild=bugs; break;
    }

}

MXML code:

<mx:ComboBox id="modelsCombo" dataProvider="{models}" selectedIndex="0"   
    change="changeViewStackModel()"/>
    <mx:ViewStack x="29.25" y="55" id="modelViewStack" width="90%" height="200">
        <mx:Canvas id="tasks" label="Tasks"><mx:Label text="Tasks stack view!!"/>
        </mx:Canvas>
        <mx:Canvas id="users" label="Users"><mx:Label text="Users stack view!!"/>
        </mx:Canvas>
        <mx:Canvas id="bugs" label="Bugs"><mx:Label text="Bugs stack view!!"/>
        </mx:Canvas>
    </mx:ViewStack>
thejartender
  • 9,339
  • 6
  • 34
  • 51
0

Or you can bind the selectedIndex of the viewStack to the selectedIndex property of the combo:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
    <mx:ComboBox id="comboNav" dataProvider="{['canvas1','canvas2']}"/>

    <mx:ViewStack id="stackNav" width="400" height="300"
        selectedIndex="{comboNav.selectedIndex}">
        <mx:Canvas id="canvas1" width="100%" height="100%">
           <mx:Label text="Hello" horizontalCenter="0" verticalCenter="0"/>
        </mx:Canvas>
        <mx:Canvas id="canvas2" width="100%" height="100%">
           <mx:Label text="World!" horizontalCenter="0" verticalCenter="0"/>
        </mx:Canvas>
    </mx:ViewStack>

</mx:Application>
bug-a-lot
  • 2,446
  • 1
  • 22
  • 27