如何动态更改datagrid行的背景颜色? [英] How to dynamically change background colour of datagrid row?

查看:642
本文介绍了如何动态更改datagrid行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



看起来有各种各样的hack可以改变datagrid的行的背景颜色,但是所有这些都似乎在渲染时发生。 :在Adobe Flex中为datagrid行设置背景颜色



我有一个datagrid,我需要将行颜色更改为红色,然后根据绑定ArrayCollection的更改频繁返回正常。所以我正在寻找一种动态改变行颜色的方法。



任何人都可以帮助?显然,随着变化的频繁发生,如果改变行的背景颜色不是一个昂贵的过程,但是beggar不能作为选择器,这将是很好的。

解决方案

您必须编写一个自定义组件,我将包含来自我们项目的已清理代码。在您的ActionScript中的某个地方,您需要为每个列写入:

  column.itemRenderer = new ClassFactory(CellRenderer); 

以下是自定义类:

  public class CellRenderer extends Label {
override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth,unscaledHeight);
var g:Graphics = graphics;
g.clear();
每个(var object:array in arrayCollection){
if(object.rowIndex == 0){//或者你的条件是
g.beginFill(0xFFFFC0);
g.drawRect(0,0,unscaledWidth,unscaledHeight);
g.endFill();
}
}
}
}


It seems like there are various hacks out there to change the background colour of the row of a datagrid but all of them seem to happen at render time.

See: Setting background color for datagrid row in Adobe Flex

I have a datagrid where I need to change row colours to red then back to normal frequently based on changes to the bound ArrayCollection. So I'm looking for a way to change the row colours dynamically.

Can anyone help? Obviously as the changes are happening frequently it would be nice if changing the background colour of the row wasn't an expensive process, but beggar's can't be choosers.

解决方案

You'll have to write a custom component, I'll include sanitized code from our project. Somewhere in your ActionScript, you'll need to write this for each column:

column.itemRenderer = new ClassFactory(CellRenderer);

Here's the custom class:

public class CellRenderer extends Label {
    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { 
        super.updateDisplayList(unscaledWidth, unscaledHeight); 
        var g:Graphics = graphics;
        g.clear();
        for each(var object:Object in arrayCollection) {
            if (object.rowIndex == 0) { //or whatever your conditions are
                g.beginFill(0xFFFFC0); 
                g.drawRect(0, 0, unscaledWidth, unscaledHeight);
                g.endFill(); 
            }
        } 
    } 
} 

这篇关于如何动态更改datagrid行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆