Flex:菜单栏、菜单点击

发布于 2024-08-24 07:35:18 字数 725 浏览 12 评论 0原文

我不知道为什么,但我看到菜单栏上的 itemclick 事件不会触发,除非您单击子项目。

处理位于顶层并且没有子菜单项的菜单项的点击的干净方法是什么。

例如,我想在单击 MenuItem B 时触发一个事件。

<?xml version="1.0"?>
<!-- menus/MenuBarControl.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

    <mx:MenuBar id="myMenuBar" labelField="@label" itemClick="{itemClick(event)}" >
        <mx:XMLList>
            <menuitem label="MenuItem A">
                <menuitem label="SubMenuItem A-1"/>
                <menuitem label="SubMenuItem A-2"/>
            </menuitem>
            <menuitem label="MenuItem B"/>
        </mx:XMLList>
    </mx:MenuBar>
</mx:Application>

I do not know why but I see that itemclick event on a menubar do not fired unless you click a sub item.

What is the clean way to handle clicks on menuitems which are on the top level and do not have sub menu items.

For example I want to fire an event whenever MenuItem B is clicked.

<?xml version="1.0"?>
<!-- menus/MenuBarControl.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

    <mx:MenuBar id="myMenuBar" labelField="@label" itemClick="{itemClick(event)}" >
        <mx:XMLList>
            <menuitem label="MenuItem A">
                <menuitem label="SubMenuItem A-1"/>
                <menuitem label="SubMenuItem A-2"/>
            </menuitem>
            <menuitem label="MenuItem B"/>
        </mx:XMLList>
    </mx:MenuBar>
</mx:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏林苍穹下 2024-08-31 07:35:18

我想这会有所帮助,

protected function myMenuBar_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            stackIndex=event.target.data.@index;
        }

protected function myMenuBar_itemClickHandler(event:MenuEvent):void
        {
            // TODO Auto-generated method stub
            stackIndex=event.item.@index;
        }

在下面的菜单栏中,

<mx:MenuBar id="myMenuBar" labelField="@label" click="myMenuBar_clickHandler(event)" itemClick="myMenuBar_itemClickHandler(event)" >   
    <mx:XMLList>   
        <menuitem label="MenuItem A" index="0">   
            <menuitem label="SubMenuItem A-1" index="0-0"/>   
            <menuitem label="SubMenuItem A-2" index="0-1"/>   
        </menuitem>   
        <menuitem label="MenuItem B" index="1"/>   
    </mx:XMLList>   
</mx:MenuBar>  

我还添加了一个属性“索引”。作为 XML,我们可以在其中添加除某些关键字之外的任何元素。

“ItemClick”事件将处理子项,“Click”事件将处理顶级菜单栏项(父项)。

希望这有帮助。

问候,
普拉卡什

Guess this can help,

protected function myMenuBar_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            stackIndex=event.target.data.@index;
        }

protected function myMenuBar_itemClickHandler(event:MenuEvent):void
        {
            // TODO Auto-generated method stub
            stackIndex=event.item.@index;
        }

Fof the following menubar,

<mx:MenuBar id="myMenuBar" labelField="@label" click="myMenuBar_clickHandler(event)" itemClick="myMenuBar_itemClickHandler(event)" >   
    <mx:XMLList>   
        <menuitem label="MenuItem A" index="0">   
            <menuitem label="SubMenuItem A-1" index="0-0"/>   
            <menuitem label="SubMenuItem A-2" index="0-1"/>   
        </menuitem>   
        <menuitem label="MenuItem B" index="1"/>   
    </mx:XMLList>   
</mx:MenuBar>  

Here i ve also added a property 'index'. As an XML we can add any element in it except for some of the keywords.

The "ItemClick" event will take of the childrens, and the "Click" will take care of the toplevel menubar items(parents).

Hope this helps.

Regards,
Prakash

别念他 2024-08-31 07:35:18

此行为是设计使然。想象一下,如果它是浏览器中的菜单栏:单击文件、编辑、查看等顶级项目只会显示弹出窗口,它们不会触发任何操作。

来自 MenuBar 的 livedocs 页面< /a>

MenuBar 控件定义一个包含一个或多个菜单项的水平顶级菜单栏。单击顶级菜单项将打开一个弹出子菜单,该子菜单是 Menu 控件的实例。

MenuBar 控件的顶级菜单栏通常始终可见。它不适合用作弹出菜单。当用户使用鼠标或键盘选择各个子菜单时,会弹出各个子菜单。

如果您必须在顶级项目单击上执行某些操作,请侦听 MenuBar 上的 click 事件,并遍历 parent 链>event.target 搜索 MenuBarItem,这是 MenuBar 顶级项目的默认项目渲染器

This behavior is by design. Think if it as the menu bar in your browser: clicking on top level items like File, Edit, View etc just shows the popup, they don't trigger any action.

From the livedocs page for MenuBar

A MenuBar control defines a horizontal, top-level menu bar that contains one or menu items. Clicking on a top-level menu item opens a pop-up submenu that is an instance of the Menu control.

The top-level menu bar of the MenuBar control is generally always visible. It is not intended for use as a pop-up menu. The individual submenus pop up as the user selects them with the mouse or keyboard.

If you must do something on top level item click, listen to the click event on the MenuBar and traverse through the parent chain of the event.target searching for a MenuBarItem, the default item renderer for top level items of a MenuBar

美煞众生 2024-08-31 07:35:18

flex 开箱即用的菜单栏有很多限制。例如我不能有像这样的嵌套子菜单
顶部菜单
- 1级菜单
-2级菜单
- 3级菜单......
为了解决这个问题,需要在每个子菜单中添加一个额外的菜单,这确实很痛苦。

The menu bar which flex gives out of the box has lot of limitations .For instance i cannot have a nested submenu like
top_menu
-level 1 menu
-level 2 menu
-level 3 menu.....
to counter this one need to add an extra menu in each sub-menu which is pain indeed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文