开发者

Programatically enable/disable menuBar buttons in Flex 4

I have the following XML in my Flex4 (AIR) project that defines the start of my menu interface:

<mx:MenuBar x="0" y="0" width="100%" id="myMenuBar" labelField="@label" itemClick="menuChange(event)">
    <mx:dataProvider>
    <s:XMLListCollection>
    <fx:XMLList xmlns="">
        <menu label="File">
            <item label="New"/>
            <item label="Load"/>
            <item label="Save" enabled="false"/>
        </menu>
        <menu label="Help">
            <item label="About"/>
        </menu>
    &l开发者_StackOverflow社区t;/fx:XMLList>
    </s:XMLListCollection>
    </mx:dataProvider>
</mx:MenuBar>

I am trying to find the syntax that will let me set the save button to enabled=true after a file has been loaded by clicking "Load", however I can't figure out the syntax, can someone make a suggestion please.

Currently the way that button clicks are detected is by a Switch/Case testing the String result of the MenuEvent event.item.@label. Maybe this isn't the best way?


Answering my own question .... yet again. What is it with the stuff I'm doing nobody seems to want to answer???

Anyway, here it is:

It turns out that since the menubar is defined in XML and it's completely arbitrary, it depends totally on how you decide to define your menu, in my case, according to the menu XML above, the syntax to changethe "eanabled" state of the Save button would be as follows.

menubarXML.item.(@label=="Save").@enabled = "true";

where enubarXML is an XMLListCollection holding the XMLList which I redefined seperately.

[Bindable]
        public var menuBarCollection:XMLListCollection;

        private var menubarXML:XMLList =<>
            <menu label="File">
                <menuitem label="New" data="1A"/>
                <menuitem label="Open" data="1B"/>
                <menuitem label="Save" data="1C" enabled="false"/>
            </menu>
            <menu label="Help" data="2A">
                <menuitem label="About" data="2A"/>
            </menu>
            </>;

Then call this function on the apps creation complete:

private function initCollections():void {
            menuBarCollection = new XMLListCollection(menubarXML);
        }

and of course the XML definition of the menubar (customise this as required):

<mx:MenuBar id="myMenuBar" labelField="@label" itemClick="menuChange(event)" dataProvider="{menuBarCollection}"/>

Hope someone finds the fruits of my labour useful.


private var actions : Object = {
    mi_new : mi_new,
    mi_open : mi_open
};

protected function myMenuBar_itemClickHandler(event:MenuEvent):void
{
    actions[event.item.@action]();
}

private function mi_new() : void {
    // do new stuff
}

private function mi_open() : void {
    // do open stuff
}

private var menubarXML:XMLList =<>
    <menu label="File">
        <menuitem label="New" data="1A" action="mi_new"/>
        <menuitem label="Open" data="1B" action="mi_open"/>
        <menuitem label="Save" data="1C" enabled="false"/>
    </menu>
    <menu label="Help" data="2A">
        <menuitem label="About" data="2A"/>
    </menu>
    </>;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜