Setting background color for datagrid row in Adobe

2019-01-12 09:38发布

I need to programmatically change the background color for a single row in a datagrid in Flex. I've scoured the Net and found reference to "dg.setPropertiesAt," which is not a supported method (according to the compiler). Also, there are suggestions to extend the dg's "drawRowBackground" method but I need to set the background externally (not from logic inside the dg).

Any and all suggestions welcome.

TIA, Bob

5条回答
别忘想泡老子
3楼-- · 2019-01-12 10:08
dg.setPropertiesAt(3, {backgroundColor:0xFF0000});

Where dg is your datagrid and the number 3 is the row color of your grid.

查看更多
贼婆χ
4楼-- · 2019-01-12 10:12

I was wondering the same thing just a couple of days ago. If you have the Pro version of Flex, its AdvancedDataGrid has the built-in "styleFunction" property to handle this. If you've only got the regular DataGrid handy, this might help:

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

A comment there links to documentation of styleFunction:

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

Beyond that, Stiggler's suggestion for using an itemRenderer is your other recourse.

查看更多
男人必须洒脱
5楼-- · 2019-01-12 10:18

Use this with 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>

In your app code:

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

The key element is IGridVisualElement interface which lets you bind to your dataProvider. This interface is called by GridLayout. See: http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/spark/src/spark/components/gridClasses/GridLayout.as. You can use any IVisualElement as a background renderer, but with s:DefaultGridItemRenderer you have some functionality out of the box.

Hope this helps

查看更多
何必那么认真
6楼-- · 2019-01-12 10:19

I managed it by extending the DataGrid class and creating my own method, like this:

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);
}

This was inspired by the drawRowBackgrounds method of the datagrid class.

Hope it helps.

查看更多
登录 后发表回答