Here is a complete example:
file ApplicationEntry.mxml
----------------------------------------------- snip
---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns="*"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var myData : ArrayCollection;
private function onCreationComplete() : void
{
trace("ApplicationEntry::onCreationComplete");
myData = initData();
}
private function initData() : ArrayCollection
{
var result : ArrayCollection = new ArrayCollection();
for( var i : uint = 0; i < 5; i++ )
{
result.addItem( createItem( i ));
}
return result;
}
private function createItem( index: uint ) : Object
{
var color : uint = Math.floor( Math.random() * 0xffffff );
var label : String = "test " + index;
var result : Object = new Object();
result.label = "test " + index;
result.color = color;
return result;
}
]]>
</mx:Script>
<mx:DataGrid dataProvider="{ myData }">
<mx:columns>
<mx:DataGridColumn dataField="label"
itemRenderer="MyItemRenderer"/>
<mx:DataGridColumn dataField="color"
itemRenderer="MyItemRenderer"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
----------------------------------------------- snip
---------------------------------------------
//file MyItemRenderer.mxml
----------------------------------------------- snip
---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml"
color="{ data.color }" text="{ getLabel( data ) }">
<mx:Script>
<![CDATA[
private function getLabel( data : Object ) : String
{
return listData.label;
}
]]>
</mx:Script>
</mx:Label>
----------------------------------------------- snip
---------------------------------------------
On 12/22/06, Ralf Bokelberg <[EMAIL PROTECTED]> wrote:
The common solution is to add the color or style name to the items of your
dataprovider and bind to this property inside your itemrenderer.
Cheers,
Ralf
On 12/21/06, retrogamer4ever <[EMAIL PROTECTED]> wrote:
>
> Okay I am going to try and be as clear as possible with this, so
> bare with me.
>
> What I am trying to do is make it so that when my arraycollection
> objects that I have retrieved from a web service are loaded in (by
> using the "dataProvider" attribute) my dataGrid that some sort of
> code will take place changing the color of each line of text (all
> the objects stored in the array collection are string types) and
> display each rows text in a different color.
>
> Now after looking into it, it seems the only way to alter the color
> of the text is to use some sort of style or format but it seems it
> only effects text in a "textArea, textField, textInput" etc...
> SOOO I figured why not create a itemRenderer that contains one of
> those and put it into the dataGrid... Which I did and still can't
> figure out a way to make it so you can dynamically alter the color
> based on a set of rbg values stored in a array the same size as the
> rowCount of the datagrid.
>
> so I am rather stumpped in what to do.. I DON'T want to change the
> background color of each row, so alternatingItemColor is out of the
> question, I just want the text displayed in each row to be a
> different color.... And I want all this color changing to happen
> either before the data is inputted into the dataGrid (manipulating
> the arraycollection some how..) or when its all already in there, it
> all needs to happen in code no user interaction.
>
> I was thinking perhaps maybe I could create a item Renderer object
> that contains the compenent (the textArea in it) and just make a
> array of item Renderer objects and pass those into the dataGrid, but
> I don't think that is possible.
>
> ANY IDEAS AT ALL!! On how to change the color of the text in each
> row of the datagrid to a different color would be a HUGE, HUGE!!!
> help. Or any info on how to setup a datagrid listener that listens
> for when a object (a row) from the arraycollection is added to the
> datagrid... Perhaps I could use that info some how to my
> advantage.
>
> email me, if you like I don't care I just need a answer to this its
> driving me crazy! I can change the background row color based on a
> array of rgb values but I can't change the color of the item in that
> row based on array of rgb values, ARG!
>
> thanx in advanced.
>
>
>
--
Ralf Bokelberg <[EMAIL PROTECTED]>
Flex & Flash Consultant based in Cologne/Germany
--
Ralf Bokelberg <[EMAIL PROTECTED]>
Flex & Flash Consultant based in Cologne/Germany