0

我有一个菜单栏对象,其中一个菜单有子菜单。当我单击某些子菜单时,我想用它的图标替换所选子菜单图标的父级。

4

1 回答 1

2

Flex 4/4.5 中的菜单栏仍然是 Flex 3 时代的 mx 组件。

菜单栏是数据驱动的,所以可以修改父菜单对应的数据项,改变其图标数据,将整个菜单数据重新赋值给组件。下面的伪代码可以表示所描述的逻辑:

private var _menuBarData:Object;
public var myMenuBar:MenuBar;

override protected function createChildren():void
{
    super.createChildren();

    if ( myMenuBar == null )
    {
        myMenuBar = new MenuBar();
        addChild(myMenuBar);
    }

    myMenuBar.dataProvider = _menuBarData;  // assume that _menuBarData is populated already
    myMenuBar.removeEventListener(MenuEvent.CHANGE, myMenuBar_change);
    myMenuBar.addEventListener(MenuEvent.CHANGE, myMenuBar_change);
}


private function myMenuBar_change( event:MenuEvent ):void
{
    var itemData:Object = event.item;

    if ( itemData == null )
    {
        return;
    }

    var iconData:Object = itemData[iconField];

    var parentData:Object = findParentData(itemData);
    if ( parentData )
    {
        parentData[iconField] = iconData;

        event.menuBar.dataProvider = _menuBarData;  // reassign _menuBarData as it is updated
    }
}


private function findParentData(itemData:Object):Object
{
    // traverse through _menuBarData object and find out the parent of itemData
    // this highly depends on how your data structure is designed
}
于 2012-10-30T17:36:31.170 回答