为什么,tree树形控件没有MouseEvent.RIGHT_CLICK监听事件呢?如何解决
我要实现:右键单击每一个树形控件的节点,弹出不同的操作菜单
如果可以用其他方法更好,还请指点一哈 谢谢!!
[解决办法]
妹子,Flex里面是没有右键监听的,AIR里面有。
你这个实现有点麻烦,要不是你问问题,我真就不帮你弄了,哈哈。
看看效果:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"><mx:Script> <![CDATA[ import mx.controls.Alert; import mx.collections.ArrayCollection; import flash.display.Sprite; import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; import flash.events.ContextMenuEvent; //在全局上,定义三个栏目 private var menu1:ContextMenuItem; private var menu2:ContextMenuItem; private var menu3:ContextMenuItem; [Bindable] public var treeStore:ArrayCollection = new ArrayCollection(); public function init():void { treeStore = new ArrayCollection( [ {label:"100", children:[ {label:"1001"},{label:"1002"}]}, {label:"200"} ]); insertContextMenu(treeStore.source, 0); } private function insertContextMenu(dataStore:Array, level:int=0):void { for each (var item:Object in dataStore) { var context:Array = obtainContext(level); var treeMenu:ContextMenu = buildContextMenu(context); item.contextMenu= treeMenu; if (item.children != null) { insertContextMenu(item.children as Array, level+1); } } } private function obtainContext(level:int):Array { var context:Array = []; var contextItem:Object = {}; contextItem.menuLabel = "第" + (level+1) + "层的树的右键选项"; contextItem.selectFunction = selectFunction; context.push(contextItem); return context; } private function buildContextMenu(context:Array):ContextMenu { var treeMenu:ContextMenu = new ContextMenu(); var menuItem:ContextMenuItem = null; var menuLabel:String = null; var selectFunction:Function = null; for each (var item:Object in context) { menuLabel = item.menuLabel; selectFunction = item.selectFunction; menuItem = new ContextMenuItem(menuLabel); menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, selectFunction); treeMenu.customItems.push(menuItem); } treeMenu.hideBuiltInItems(); return treeMenu; } private function selectFunction(event:ContextMenuEvent):void { } ]]></mx:Script> <mx:Tree id="tree" dataProvider="{treeStore}" itemRenderer="MyTreeItemRenderer" labelField="label" showRoot="false" width="300" height="400"/></mx:Application>
[解决办法]
arraycollection里面的项可以理解为object,如果你上面这个问题是指一个Arraycllection[0]做修改的话,应该就可以直接写(Arraycllection[0] as Object).lebal4="重庆"(大概这意思吧,不知道语法对不对)