如何更改DataGrid中某一行的背景色是一個(gè)被經(jīng)常問(wèn)的問(wèn)題。這個(gè)在Flex2.0中很簡(jiǎn)單,只需按照下面的步驟做:
1.創(chuàng )建一個(gè)擴展自 mx.controls.DataGrid 的類(lèi)。這個(gè)類(lèi)可以是MXML文件或者ActionScript文件,你可以根據自己的習慣創(chuàng )建。
2.覆寫(xiě) protected 方法
drawRowBackground : override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number,height:Number, color:uint, dataIndex:int):void {// 這里可以做一些對數據的判斷,然后更改相應的顏色。比如color = 0xFF0000; // 調用super函數來(lái)執行更改。 super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);}
3.在你的程序中用你新建的類(lèi)替代 <mx:DataGrid>。
在 drawRowBackground 方法中你可以對數據做一些判斷。dataIndex 參數可以用來(lái)查看dataProvider 中某一行所顯示的數據。例如:假設你想要的數值大于1000的行都顯示為綠色:
var item:Object = (dataProvider as ArrayCollection).getItemAt(dataIndex);if( item.quantity > 1000 ) color = 0×00FF00;
就這么簡(jiǎn)單。