Search code examples
actionscript-3flashapache-flexactionscriptflex4

Actionscript 3 setStyle is not a function


I am trying to style a Flex 4 GridItem using actionscript, I have tried the following:

<mx:VBox 
    height="878" width="1920"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*" creationComplete="addStyles()">


    <mx:Script>
        <![CDATA[


            public var selectedLot:String = "";

            private function addStyles():void
            {
                testBorder.setStyle("borderThickness", "3");
            }

but I get the following error:

setStyle is not a function.

Am I missing something?

The GridItem is inside a repeater.

Here is my GridItem:

<mx:GridItem id="testBorder" width="101" height="52.25" horizontalAlign="center" verticalAlign="middle" borderStyle="solid" borderColor="gray">
                                                                            <mx:Image source="assets/ruler-icon.png" />
                                                                            <s:Label text="{r.currentItem.sqft}" fontSize="10" color="#808080" fontFamily="Helvetica" />
                                                                        </mx:GridItem>

Solution

  • When using a repeater the GridItem's id will not be the same. To access any item inside a repeater you have to specify an index which is correspondent to the repeated item.

    Example: Array consists of ["Audi", "BMW"], we set this array to your repeater's dataProvider, then to access "Audi"'s grid item and set its style, we have to use:

    testBorder[0].setStyle("borderThickness", "3");
    

    Additionally, an important point to consider, the VBox "creationComplete" can be executed before the repeater is fully built, therefore, the best place to call your function "addStyles" is in the repeater's "repeatEnd" event i.e (repeatEnd="setTransactionPropertyType()").

    Hope this helps,

    Goodluck.