Flex问题:我可以在Flex中使用ComboBox来更改视图堆栈吗?

时间:2009-05-21 20:57:14

标签: flex combobox viewstack

我试图在flex中使用一个带有数组的组合框来更改为视图堆栈中的画布。 我可以使用一些自定义动作脚本吗?我正在设计的UI可以从中受益。

3 个答案:

答案 0 :(得分:3)

这是一个演示应用程序的小型演示应用程序:

<?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>

您可以更改onComboBoxChange()中的逻辑以使用selectedIndex:

stackNav.selectedIndex = comboNav.selectedIndex;

答案 1 :(得分:0)

或者您可以将viewStack的selectedIndex绑定到组合的selectedIndex属性:

<?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>

答案 2 :(得分:0)

您可以使用此解决方案,但要注意您不能用空格来拼写单词 (尝试将“canvas1”更改为“canvas 1”),您将看到:

[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代码:

<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>