我有一个菜单栏对象,其中一个菜单有子菜单。 我想点击一些子菜单,用它的图标替换所选子菜单图标的父级。
答案 0 :(得分: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
}