我有一個由多個應用程序共享的ItemRenderer(在DataGrid中),我想向它添加一個上下文菜單(而不是在每個應用程序中)。渲染是從Canvas類派生,並創建上下文菜單的代碼看起來是這樣的:Flex 3:可能將上下文菜單添加到DataGrid中的ItemRenderer?
var menuItem:ContextMenuItem = new ContextMenuItem("Test");
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextCallback);
var customContextMenu:ContextMenu = new ContextMenu();
customContextMenu.hideBuiltInItems(); //hide flash menu
customContextMenu.customItems.push(menuItem);
this.contextMenu = customContextMenu;
然而,當我用鼠標右鍵單擊DataGrid中的細胞,我得到了默認的Flash上下文菜單。這不可能嗎?
編輯:這是一個完全可運行的例子,不顯示上下文菜單中的:
Application.mxml:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Model id="items">
<items>
<item label="Item 1" data="i001" propertyA="Item 1.A" propertyB="Item 1.B" />
<item label="Item 2" data="i002" propertyA="Item 2.A" propertyB="Item 2.B" />
<item label="Item 3" data="i003" propertyA="Item 3.A" propertyB="Item 3.B" />
<item label="Item 4" data="i004" propertyA="Item 4.A" propertyB="Item 4.B" />
<item label="Item 5" data="i005" propertyA="Item 5.A" propertyB="Item 5.B" />
<item label="Item 6" data="i006" propertyA="Item 6.A" propertyB="Item 6.B" />
<item label="Item 7" data="i007" propertyA="Item 7.A" propertyB="Item 7.B" />
<item label="Item 8" data="i008" propertyA="Item 8.A" propertyB="Item 8.B" />
</items>
</mx:Model>
<mx:DataGrid id="dataGrid" width="400" dataProvider="{items.item}">
<mx:columns>
<mx:DataGridColumn headerText="No Menu" dataField="label" />
<mx:DataGridColumn headerText="Menu" dataField="propertyA" itemRenderer="canvasRenderer"/>
</mx:columns>
</mx:DataGrid>
canvasRenderer.mxml:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" verticalScrollPolicy="off" horizontalScrollPolicy="off" resize="this.setSize()" creationComplete="init()" implements="mx.controls.listClasses.IDropInListItemRenderer">
<mx:Script>
<![CDATA[
import mx.controls.listClasses.ListData;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.events.FlexEvent;
private var _listData:DataGridListData;
private function init():void {
var menuItem:ContextMenuItem = new ContextMenuItem("Copy", true);
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function():void {
trace("selected");
});
var menu:ContextMenu = new ContextMenu();
menu.hideBuiltInItems(); //hide flash menu
menu.customItems.push(menuItem);
this.contextMenu = menu;
}
public override function set data(value:Object):void {
super.data = value;
if(_listData && myLabel) {
var text:String = _listData.label ? _listData.label : value[_listData.dataField];
myLabel.text = text;
myLabel.toolTip = text;
}
}
public function get listData():BaseListData { return _listData; }
public function set listData(value:BaseListData):void { _listData = DataGridListData(value); }
public function setSize():void { myLabel.width = this.width; }
]]>
</mx:Script>
<mx:Label id="myLabel" truncateToFit="true"/>
我提供了一個可運行的例子。它看起來好像和它被包裹在畫布裏面有關,但那只是猜測 – gmoniey 2010-05-19 07:10:07
將文本從「複製」更改爲其他內容,並且您的代碼開始奇蹟般地工作。 「複製」必須是保留字或類似的東西。 – JeffryHouser 2010-05-19 13:44:24
啊......非常令人沮喪。假設它是我的錯。我實際上更改了第一個示例中的文本,但從未嘗試過運行該代碼。 感謝您的支持。 – gmoniey 2010-05-19 16:31:41