在Adobe Flex中为datagrid行设置背景颜色

问题描述:

我需要以编程方式更改Flex中数据网格中单个行的背景颜色。我搜索了网络,发现对“dg.setPropertiesAt”的引用,它不是受支持的方法(根据编译器)。此外,还有一些建议可以扩展dg的“drawRowBackground”方法,但我需要在外部设置背景(而不是从dg内部的逻辑)。在Adobe Flex中为datagrid行设置背景颜色

欢迎任何和所有建议。

TIA, 鲍勃

+0

看起来像这样会做...谢谢,鲍勃 – 2009-04-14 16:53:04

我只是一个前两天知道同样的事情。如果你有Pro版本的Flex,它的AdvancedDataGrid有内置的“styleFunction”属性来处理这个。如果你只得到了正规的DataGrid得心应手,这可能帮助:

http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=12548

注释有链接到styleFunction的文档:

http://livedocs.adobe.com/flex/3/langref/mx/controls/advancedDataGridClasses/AdvancedDataGridBase.html#styleFunction

除此之外,Stiggler的建议使用的itemRenderer是您的其他追索权。

dg.setPropertiesAt(3, {backgroundColor:0xFF0000}); 

其中dg是您的数据网格,3是网格的行颜色。

我设法它通过扩展DataGrid类和创造我自己的方法,这样的:

public function paintRow(rowNumber:Number,color:uint):void{ 
var rowBGs:Sprite=Sprite(listContent.getChildByName("rowBGs")); 
drawRowBackground(rowBGs,rowNumber,listContent.rowInfo[rowNumber].y,listContent.rowInfo[rowNumber].height,color,null); 
} 

这是由DataGrid类的drawRowBackgrounds方法的启发。

希望它有帮助。

使用此与spark.DataGrid

DataGridRowBackground.mxml:

<?xml version="1.0" encoding="utf-8"?> 
<s:DefaultGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/halo" 
    implements="spark.components.gridClasses.IGridVisualElement" 
    backgroundColor="{data.color}" background="true"> 

    <fx:Script> 
     <![CDATA[ 

    import spark.components.Grid; 

    public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void 
    { 
     if (!grid.dataProvider || rowIndex >= grid.dataProvider.length) 
      data = null; 
     else 
      data = grid.dataProvider.getItemAt(rowIndex); 
    } 

     ]]> 
    </fx:Script> 
</s:DefaultGridItemRenderer> 

在你的应用程序代码:

<s:DataGrid> 
    <s:rowBackground> 
     <fx:Component><my:DataGridRowBackground /></fx:Component> 
    </s:rowBackground> 
</s:DataGrid> 

的关键要素是IGridVisualElement接口,它可以让您绑定到您的数据提供程序。 该接口由GridLayout调用。请参阅:http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/spark/src/spark/components/gridClasses/GridLayout.as。你可以使用任何IVisualElement作为后台渲染器,但是使用s:DefaultGridItemRenderer,你可以使用一些开箱即用的功能。

希望这会有所帮助