弄清楚了。当标题被鼠标悬停时,我只是禁用了预定的菜单项。如果s存在于数组中,ContextMenuItem
则它们将被禁用。caption
hiddenContextItems
我提供了一个使用我ExtendedAdvancedDataGridHeaderRenderer
创建的对象的示例,以防有人感兴趣。
下面的示例生成一个包含三列的图表:红色、绿色和蓝色。第一行是该颜色的最高对比度,在该对比度级别之后的每一行都会降低。这会在网格上产生渐变效果。您可以通过右键单击单元格将颜色的十六进制值复制到剪贴板。
ExtendedAdvancedDataGridHeaderRenderer.as
package components {
import flash.events.MouseEvent;
import flash.ui.ContextMenuItem;
import mx.controls.AdvancedDataGrid;
import mx.controls.advancedDataGridClasses.AdvancedDataGridHeaderRenderer;
public class ExtendedAdvancedDataGridHeaderRenderer extends AdvancedDataGridHeaderRenderer {
private var _hiddenContextMenuItems:Array; // of String
public function get hiddenContextMenuItems():Array {
return _hiddenContextMenuItems;
}
public function set hiddenContextMenuItems(value:Array):void {
_hiddenContextMenuItems = value;
}
public function ExtendedAdvancedDataGridHeaderRenderer() {
super();
hiddenContextMenuItems = [ExtendedAdvancedDataGrid.MENU_COPY];
this.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
this.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
}
protected function onRollOver(event:MouseEvent):void {
disableMenuItems(hiddenContextMenuItems);
}
protected function onRollOut(event:MouseEvent):void {
disableMenuItems([]);
}
private function disableMenuItems(arr:Array):void {
if (arr != null && this.owner is AdvancedDataGrid) {
var adg:AdvancedDataGrid = this.owner as AdvancedDataGrid;
if (adg.contextMenu != null && adg.contextMenu.customItems != null) {
for each (var item:ContextMenuItem in adg.contextMenu.customItems) {
item.enabled = !arrContainsStr(arr, item.caption);
}
}
}
}
private function arrContainsStr(arr:Array, str:String):Boolean {
for each (var arrStr:String in arr)
if (arrStr == str)
return true;
return false;
}
}
}
ExtendedAdvancedDataGridItemRenderer.as
package components {
import mx.controls.advancedDataGridClasses.AdvancedDataGridItemRenderer;
public class ExtendedAdvancedDataGridItemRenderer
extends AdvancedDataGridItemRenderer {
public function ExtendedAdvancedDataGridItemRenderer() {
super();
}
override public function set data(value:Object):void {
super.data = value;
this.background = true;
var bgColor:uint = parseInt(listData.label, 16);
var fgColor:uint = bgColor ^ 0xFFFFFF;
this.backgroundColor = bgColor;
this.setStyle('color', fgColor);
this.setStyle('textRollOverColor', 0xFFFFFF);
}
}
}
ExtendedAdvancedDataGridColumn.as
package components {
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.ClassFactory;
public class ExtendedAdvancedDataGridColumn extends AdvancedDataGridColumn {
public function ExtendedAdvancedDataGridColumn(columnName:String=null) {
super(columnName);
headerRenderer = new ClassFactory(ExtendedAdvancedDataGridHeaderRenderer);
itemRenderer = new ClassFactory(ExtendedAdvancedDataGridItemRenderer);
}
}
}
ExtendedAdvancedDataGrid.as
package components {
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.system.System;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import mx.controls.AdvancedDataGrid;
import mx.controls.advancedDataGridClasses.AdvancedDataGridItemRenderer;
import mx.events.FlexEvent;
import mx.utils.StringUtil;
public class ExtendedAdvancedDataGrid extends AdvancedDataGrid {
public static const MENU_COPY:String = 'Copy Hex Value';
public function ExtendedAdvancedDataGrid() {
super();
this.addEventListener(FlexEvent.INITIALIZE, onInitialize);
}
protected function onInitialize(event:FlexEvent):void {
contextMenu = new ContextMenu();
contextMenu.hideBuiltInItems();
var item:ContextMenuItem = new ContextMenuItem(MENU_COPY);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, copyCellContents);
contextMenu.customItems.push(item);
}
protected function copyCellContents(event:ContextMenuEvent):void {
if (event.mouseTarget is AdvancedDataGridItemRenderer) {
var text:String = (event.mouseTarget as AdvancedDataGridItemRenderer).text;
System.setClipboard(StringUtil.substitute('#{0}', text));
}
}
}
}
应用程序.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
xmlns:components="components.*"
initialize="onInitialize(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.utils.StringUtil;
[Bindable] protected var gridData:ArrayCollection;
protected function onInitialize(event:FlexEvent):void {
gridData = new ArrayCollection();
for (var i:uint = 15; i >= 0; i--) {
var hexVal:String = intVal.toString(i)
var red:String = StringUtil.substitute('{0}{0}0000', hexVal);
var green:String = StringUtil.substitute('00{0}{0}00', hexVal);
var blue:String = StringUtil.substitute('0000{0}{0}', hexVal);
gridData.addItem({'red': red, 'green': green, 'blue': blue});
}
grid.invalidateDisplayList();
}
]]>
</fx:Script>
<s:BorderContainer horizontalCenter="0">
<components:ExtendedAdvancedDataGrid id="grid" dataProvider="{gridData}"
rowCount="{gridData.length + 1}">
<components:columns>
<components:ExtendedAdvancedDataGridColumn dataField="red" headerText="Red"/>
<components:ExtendedAdvancedDataGridColumn dataField="green" headerText="Green"/>
<components:ExtendedAdvancedDataGridColumn dataField="blue" headerText="Blue"/>
</components:columns>
</components:ExtendedAdvancedDataGrid>
</s:BorderContainer>
</s:Application>