今天,发现一个Flex的Bug:
当你设置一个DataGrid的 showHeaders="false",则当你在<mx:DataGridColumn/>中设置backgroundColor样式时会报错:
TypeError: Error #1010: A term is undefined and has no properties.
at mx.controls::DataGrid/drawColumnBackground()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\DataGrid.as:3012]
at mx.controls::DataGrid/drawLinesAndColumnGraphics()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\DataGrid.as:3311]
at mx.controls::DataGrid/drawLinesAndColumnBackgrounds()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\DataGrid.as:3207]
……
查看了DataGrid的源代码,发现是在drawColumnBackground方法中用到了columnHeader对象,而这时可能因为showHeaders="false"而columnHeader没有相应的属性值了。
因为是要写一个最后一列没有边框的DataGrid,所以就继承并改写了DataGrid,另外写了个方法:
protected function drawColumnBackgroundX(s:Sprite, columnIndex:int, columnX:Number, color:uint, column:DataGridColumn):void {
var background:Shape;
background = Shape(s.getChildByName(columnIndex.toString()));
if(!background) {
background = new FlexShape();
s.addChild(background);
background.name = columnIndex.toString();
}
var g:Graphics = background.graphics;
g.clear();
g.beginFill(color);
var lastRow:Object = rowInfo[listItems.length - 1];
// var columnHeader:DataGridHeader = (s.parent == lockedColumnContent) ? DataGridHeader(lockedColumnHeader) : DataGridHeader(header);
// var xx:Number = columnHeader.rendererArray[columnIndex].x
var xx:Number = columnX;
var yy:Number = rowInfo[0].y;
// Height is usually as tall is the items in the row, but not if
// it would extend below the bottom of listContent
var height:Number = Math.min(lastRow.y + lastRow.height, listContent.height - yy);
g.drawRect(xx, yy, column.width, listContent.height - yy);
g.endFill();
}
同时改写drawLinesAndColumnGraphics(contentHolder:ListBaseContentHolder, visibleColumns:Array, separators:Object),在其中使用:drawColumnBackgroundX(colBGs, i, xx, Number(bgCol), col);来渲染背景色。