<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.events.ListEvent; [Bindable] public var treeMenu:ContextMenu = new ContextMenu(); public var lastTreeItem:ListEvent; public function init():void { treeMenu.addEventListener(ContextMenuEvent.MENU_SELECT, contextMenuTriggered); treeMenu.hideBuiltInItems(); } public function contextMenuTriggered(event:ContextMenuEvent):void { treeMenu.customItems = new Array(); if( lastTreeItem != null ) { if( lastTreeItem.itemRenderer.data.@TargetType == "Parent" ){ var addParentMenuItem:ContextMenuItem = new ContextMenuItem('Add Parent'); treeMenu.customItems.push(addParentMenuItem); } else { var addChildMenuItem:ContextMenuItem = new ContextMenuItem('Add Child'); treeMenu.customItems.push(addChildMenuItem); } } } public function setLastTreeItem(event:ListEvent):void { lastTreeItem = event; } ]]> </mx:Script> <mx:Tree id="myTree" width="100%" height="100%" labelField="@Name" dataProvider="{treeData}" contextMenu="{treeMenu}" itemRollOver="setLastTreeItem(event)" /> <mx:XMLListCollection id="treeData" source="{nodes.Node}" /> <mx:XML id="nodes"> <root> <Node Name="Parent 1" TargetType="Parent"> <Node Name="Parent 1 - Child 1" TargetType="Child"/> </Node> <Node Name="Parent 2" TargetId="3" TargetType="Parent"> <Node Name="Parent 2 - Child 1" TargetType="Child"/> <Node Name="Parent 2 - Child 2" TargetType="Child"/> </Node> <Node Name="Parent 3" TargetType="Parent"> <Node Name="Parent 3 - Child 1" TargetType="Child"/> <Node Name="Parent 3 - Child 2" TargetType="Child"/> <Node Name="Parent 3 - Child 3" TargetType="Child"/> </Node> </root> </mx:XML> </mx:Application>