Add Context menu in Datagrid
How we add context menu in datagrid column header. Thanks
Context Menus require right click, and they capture the MouseEvent.CLICK
event before you can (haven't tested it with event priority
however, so try that).
So you need to add a flash.events.ContextMenuEvent
handler to the DataGrid (or a part of the datagrid like the header). Or you can add a global one (that's what I do).
Then check what the current target is under the mouse. You probably wont get the exact item (DisplayObject/UIComponent, etc) you're looking for just checking the event.relatedObject property and such, so your best bet is to use stage.getObjectsUnderPoint(new Point(stage.mouseX, stage.mouseY)
.
Then once you get the object (or you find "this object is the one I want", you can set the context menu dynamically).
Something like this:
protected function updateTarget(event:ContextMenuEvent):void
{
var targets:Array = stage.getObjectsUnderPoint(new Point(stage.mouseX, stage.mouseY))// or less accurately, event.mouseTarget;
var target:DisplayObject = targets[targets.length - 1];
if (target)
{
// array of ContextMenuItem objects based on the target
var contextMenuItems:Array = getMyContextMenuItems(target);
if (contextMenuItems)
{
var menu:flash.ui.ContextMenu = stage.contextMenu; // or another target
var menuItems:Array = [];
var i:int = 0, n:int = contextMenuItems.length;
for (i; i < n; i++)
{
menuItems.push(contextMenuItems[i].getItem());
}
menu.customItems = menuItems;
if (hideBuiltInItems)
menu.hideBuiltInItems();
target.contextMenu = menu;
}
}
}
Customize it as you need it :).
Hope that helps.
精彩评论