<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" mouseOver="getMouseTarget(event)" applicationComplete="onAppCreated()">
<mx:Script>
?
?<![CDATA[
? import mx.events.MenuEvent;
? import mx.controls.MenuBar;
?import mx.managers.PopUpManager;
?import mx.controls.Alert;
?import flash.display.DisplayObject;
?import mx.controls.Menu;
?import flash.external.ExternalInterface;
?
?private var mouseTarget:DisplayObject;
?private var headerMenu:Menu;
?private var bodyMenu:Menu;
?
? private function onAppCreated():void{
?? ExternalInterface.addCallback("rightClick",onMouseRightButtonClicked);
? }
?
? private function onMouseRightButtonClicked():void{
?? if(headerMenu != null) headerMenu.hide();
?? if(bodyMenu != null)? bodyMenu.hide();
?? if(mouseTarget.toString().search("DataGridHeader") != -1){
??? headerMenu = Menu.createMenu(null,myMenuData,false);
?????? headerMenu.labelField="@label";
?????? headerMenu.show(stage.mouseX,stage.mouseY);
?? }else if(mouseTarget.toString().search("ListBaseContentHolder") != -1){
??? bodyMenu = Menu.createMenu(null,ContentHolder,false); //创建弹出菜单,指明弹出菜单得内容
??? bodyMenu.labelField="@label";//指明弹出菜单,显示的字段
??? bodyMenu.addEventListener(MenuEvent.ITEM_CLICK,menuHandler);? //指明点击菜单项时响应的函数
??? bodyMenu.show(stage.mouseX,stage.mouseY);
?? }
??
? }
?
? private function menuHandler(event:MenuEvent):void{
?? if(event.item.@eventName=="delete"){//delete操作
??? Alert.show("执行delete菜单操作!!!");
?? }else if(event.item.@eventName=="baidu"){
??? Alert.show("执行baidu菜单操作!!!");
?? }
? }
?
? private function getMouseTarget(event:MouseEvent):void{
?? mouseTarget =DisplayObject(event.target);
??
? }
?]]>
</mx:Script>
?<mx:DataGrid x="62" y="72" id="CDR">
?? <mx:dataProvider>
????? <mx:Object price="10"/>
????? <mx:Object p="20"/>
?? </mx:dataProvider>
? <mx:columns>
?? <mx:DataGridColumn headerText="Column 1" dataField="price"/>
?? <mx:DataGridColumn headerText="Column 2" dataField="p"/>
?? <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
? </mx:columns>
?</mx:DataGrid>
?<mx:DataGrid x="425" y="72" id="CDRlIST">
? <mx:columns>
?? <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
?? <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
?? <mx:DataGridColumn headerText="Column 3" dataField="col3"/>
? </mx:columns>
?</mx:DataGrid>
? <!-- Define the menu data. This is an official example from livedoc -->
??? <mx:XML format="e4x" id="myMenuData">
??????? <root>
??????????? <menuitem label="MenuItem A">
??????????????? <menuitem label="SubMenuItem A-1" enabled="false"/>
??????????????? <menuitem label="SubMenuItem A-2"/>
??????????? </menuitem>
??????????? <menuitem label="MenuItem B" type="check" toggled="true"/>
??????????? <menuitem label="MenuItem C" type="check" toggled="false"/>
??????????? <menuitem type="separator"/>????
??????????? <menuitem label="MenuItem D">
??????????????? <menuitem label="SubMenuItem D-1" type="radio" groupName="one" />
??????????????? <menuitem label="SubMenuItem D-2" type="radio" groupName="one" toggled="true"/>
??????????????? <menuitem label="SubMenuItem D-3" type="radio" groupName="one" />
??????????? </menuitem>
??????????? <menuitem type="separator"/>?
??????????? <menuitem label="Custom View Source"/>
??????? </root>
??? </mx:XML>
???
??? <mx:XML id="ContentHolder" source="ContentHolderMenu.xml">
????
??? </mx:XML>
</mx:Application>
ContentHolderMenu.xml:
<?xml version="1.0" encoding="utf-8"?>
?<root>
??????????? <menuitem label="删除" eventName="delete"/>
?????????????? <menuitem label="搜索" eventName="search">
???????????????? <menuitem label="使用百度" eventName="baidu"/>
???????????????? <menuitem label="使用google" eventName="google"/>
?????????????? </menuitem>
</root>
</xml>
?
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aini281032/archive/2010/02/24/5322305.aspx