Flex:未显示菜单项

时间:2012-06-07 09:57:22

标签: flex menu

这是我的代码,预期菜单会显示祖父母>父母>儿童。 但是,“父级”项目未显示,而是直接在“祖父母”下显示“子级”。

<mx:Script>
    <![CDATA[
        // Import the Menu control.
        import mx.controls.Menu;

        // Create and display the Menu control.
        private function createAndShow():void {
            var myMenu:Menu = Menu.createMenu(null, myMenuData, false);
            myMenu.labelField="@label";
            myMenu.show(10, 10);
        }
    ]]>
</mx:Script>

<!-- Define the menu data. -->
<mx:XML format="e4x" id="myMenuData">
    <root>
        <menuitem label="grandparent">
            <menuitem label="parent">
                <menuitem label="child"/>
            </menuitem>
        </menuitem>
    </root>
</mx:XML>

<mx:VBox>
    <!-- Define a Button control to open the menu -->
    <mx:Button id="myButton" 
               label="Open Menu" 
               click="createAndShow();"/>
</mx:VBox>

有趣的是,当我添加第二个父级时,它确实显示了菜单。 任何人都可以解释这里发生了什么,以及如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我拿了你的代码,把它扔进了我自己的项目中,并得到了你所做的相同的结果。然后我在XML中添加了另一个父项,一切正常。我猜测,如果你只有一个父节点,那么真的不需要显示它,所以它会跳到孩子身上。 在下面添加了第二个父级:

<root>
    <menuitem label="grandparent">
        <menuitem label="parent1">
            <menuitem label="child"/>
        </menuitem>
        <menuitem label="parent2">
            <menuitem label="child"/>
        </menuitem>
    </menuitem>
</root>

答案 1 :(得分:0)

尝试以下内容: -

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:XML id="myMenuData" >
                <menuitem label="grandparent">
                    <menuitem label="Uparent">
                        <menuitem label="child"/>
                    </menuitem>
                </menuitem>
        </fx:XML>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
        // Import the Menu control.
            import mx.controls.Menu;

            // Create and display the Menu control.
            private function createAndShow():void {
                var myMenu:Menu = Menu.createMenu(null, myMenuData, true);
                myMenu.labelField="@label";
                myMenu.show(10, 10);
            }
        ]]>
    </fx:Script>

    <!-- Define the menu data. -->


    <mx:VBox>
        <!-- Define a Button control to open the menu -->
        <mx:Button id="myButton" 
                   label="Open Menu" 
                   click="createAndShow();"/>
    </mx:VBox>

</s:Application>
相关问题