以编程方式设置PopUpMenuButton的值?

时间:2016-04-15 13:32:16

标签: actionscript-3 flex flex4 flex4.6

我需要在几个mx PopUpMenuButton组件中设置一些初始值。基于this Adobe info我将组件投射为Menu并设置selectedIndex - 但它不起作用。

任何提示?该函数被调用,似乎selectedIndex已设置,但PopUpMenuButton仍显示其数据提供者中的第一项。

       private function initFontSizeData():void {
            Menu(fontSizePopUp.popUp).selectedIndex = 3; // 48 pt

            trace("index", Menu(fontSizePopUp.popUp).selectedIndex);
        }
    <mx:PopUpMenuButton id="fontSizePopUp" 
                        name="fontSizePopUp"
                        width="50" height="20"
                        labelField="label"
                        paddingLeft="-8"
                        itemClick="toolChangeHandler(event)"
                        open="canvasEnabled(false)"
                        close="canvasEnabled(true)"
                        chromeColor="#cccccc"
                        toolTip="Font Size"
                        styleName="toolPopUpStyle"
                        creationComplete="initFontSizeData()"
                        popUpStyleName="toolPopUpStyle">
        <mx:dataProvider>
            <fx:Array>
                <fx:Object fontPointSize = "18" label="18 pt"/>
                <fx:Object fontPointSize = "24" label="24 pt" />
                <fx:Object fontPointSize = "36" label="36 pt" />
                <fx:Object fontPointSize = "48" label="48 pt" />
                <fx:Object fontPointSize = "60" label="60 pt" />
                <fx:Object fontPointSize = "72" label="72 pt"/>
                <fx:Object fontPointSize = "96" label="96 pt" />
            </fx:Array>
        </mx:dataProvider>
    </mx:PopUpMenuButton>

1 个答案:

答案 0 :(得分:1)

出于某种原因我忽略了(可能只是一个bug),PopUpMenuButton控件的标签没有更新,这就是为什么你必须强制它使用以下方法之一来做到这一点:

  • 使用mx.core.mx_internal提交所选索引:
Menu(fontSizePopUp.popUp).selectedIndex = 3;
Menu(fontSizePopUp.popUp).mx_internal::commitSelectedIndex(3);
  • 调度mx.events.MenuEvent事件:
var menu_event:MenuEvent = new MenuEvent(MenuEvent.ITEM_CLICK);
    menu_event.index = 3;

Menu(fontSizePopUp.popUp).dispatchEvent(menu_event);
  • 调度mx.events.FlexEvent事件:
Menu(fontSizePopUp.popUp).selectedIndex = 3;
Menu(fontSizePopUp.popUp).dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));

希望可以提供帮助。