Flex修改DataGrid行颜色以及按行值设置行颜色

 

本文转载自:http://gis-conquer.blog.sohu.com/159939428.html 

发现Flex背景色让行的颜色改变是有些不妥之处,就是在行数较少没有填满当前的DataGrid控件时,就连空行也变了颜色。以下是不本人自定义的一个控件,通过重写drawRowBackground,对绘制的dataIndex进行判断,可以完成设置行的颜色的设置和按值设置指定的颜色:

package widgets.Utility.Control
{
 import flash.display.Sprite;
 import mx.controls.*;
 public class DataGridColored extends DataGrid
 {
  private var _rowColorFunction:Function;
  private var _customed:Boolean;//是否自定义颜色设置
  private var _customerColor:uint=0;//自定义颜色
  public function DataGridColored()
  {
   super();
  }

  public function set rowColorFunction(rowColorFunction:Function):void
  {
   this._rowColorFunction=rowColorFunction;
  }

  public function get rowColorFunction():Function
  {
   return this._rowColorFunction;
  }
  
  public function set customed(customed:Boolean):void
  {
   _customed=customed;
  }
  
  public function get customed():Boolean
  {
   return _customed;
  }
  
  public function set customerColor(customerColor:uint):void
  {
   _customerColor=customerColor;
  }
  
  public function get customerColor():uint
  {
   return _customerColor;
  }
  
  override protected function drawRowBackground(s:Sprite, rowIndex:int, y:Number, height:Number, color:uint, dataIndex:int):void
  {
   if (_customed==true)
   {
    if (this._rowColorFunction != null)
    {
     if (dataIndex < this.dataProvider.length)
     {
      var item:Object=this.dataProvider.getItemAt(dataIndex);//按指定行进行颜色设置
      color=this._rowColorFunction.call(this, item, color);
     }
    }
    else
    {
     if (this._customerColor!=0)
     {
      if (dataIndex < this.dataProvider.length)
      {
       color=this._customerColor;
      }
     }
    }
   }
   super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
  }
 }
}


使用时注意两个参数:
customed:Boolean;//是否自定义颜色设置
customerColor:uint=0;//自定义颜色
如:
this.dataGrid.customed=true;
this.dataGrid.customerColor=0x7fceff;
也可直接在MXML进行设置

按值设置指定的颜色需要指定外部回调函数rowColorFunction
如:
在MXML设置:rowColorFunction="rowColorFunction"
下面将column1列值为gisconquer的行颜色设置为0xbeff8e

private function rowColorFunction(item:Object, color:uint):uint
{

 if (item['column1'] == "gisconquer") //将column1列值为gisconquer的行颜色设置为0xbeff8e
 {
  return 0xbeff8e;

 }
 return color;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值