条件颜色更改<s:solidcolor> </s:solidcolor>

时间:2011-04-26 14:43:12

标签: flex actionscript-3 data-binding flex4 itemrenderer

我无法让DataGridItemRenderer根据包含基于data.Bld_Type值的if语句的函数将颜色值传递给<s:SolidColor。代码如下。我是Flex的新手,并不确定问题是什么,或者这是否是正确的方法。任何帮助,将不胜感激。我试过传递十六进制数字和颜色名称。都没有工作。感谢。

<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" 
  focusEnabled="true">

    <fx:Script>
        <![CDATA[
         import mx.events.FlexEvent;

        private var swatchCOL:uint;

        /** color function **/
        private function onLoad():void
        {
            if(data.Bld_Type == "Office")
                {
                swatchCOL="ee7970";
                //***swatchCOL="red";

                }    
            else if(data.Bld_Type == "Office/Warehouse")
                {
                swatchCOL="70b2ee";
                //***swatchCOL="blue";
                }
        }   
        ]]>
    </fx:Script>



    <s:Group left="10" right="10" top="10" bottom="10">
        <s:Rect width="25" height="25">
            <s:stroke>
                <s:SolidColorStroke color="green" weight="2"/>                        
            </s:stroke>
            <s:fill>
                <s:SolidColor color="swatchCOL"/>
            </s:fill>
        </s:Rect>
    </s:Group>



</s:MXDataGridItemRenderer>

3 个答案:

答案 0 :(得分:1)

尝试更改以下内容:

[Bindable]
private var swatchCOL:uint;

<s:fill>
    <s:SolidColor color="{swatchCOL}"/>
</s:fill>

最后改变颜色类型:

if(data.Bld_Type == "Office")
                {
                swatchCOL=0xee7970;
                //***swatchCOL="red";

                }    
            else if(data.Bld_Type == "Office/Warehouse")
                {
                swatchCOL=0x70b2ee;
                //***swatchCOL="blue";
                }

答案 1 :(得分:0)

这是简单的方法:

override public function set data(value:Object):void {
    super.data = value;

    if(data.Bld_Type == "Office")
        swatchCOL.color = 0xee7970;
    else if(data.Bld_Type == "Office/Warehouse")
        swatchCOL.color = 0x70b2ee;
}

无需绑定,因为只要数据更改为您的项呈示器更改,就会调用数据设置器。此外,您需要设置“颜色”成员而不是SolidColor对象本身。您可以通过将值加上“0x”来指定十六进制颜色。

希望有所帮助。

答案 2 :(得分:0)

选项a)使用(例如,如果颜色不是动态生成的):

<s:states>
    <s:State name="stateA" />
    <s:State name="stateB" />
</s:states>
<!-- somewhere else in your code -->
<s:Rect id="bgFill" top="0" right="0" left="0" bottom="0">
    <s:fill>
        <s:SolidColor color.stateA="#313131" color.stateB="#f4dede" />
    </s:fill>
</s:Rect>
<!-- somewhere else in your code -->
<fx:Script>
<![CDATA[
if(data.Bld_Type == "Office")
            {
            this.currentState = "stateA"
            //***swatchCOL="red";

            }    
        else if(data.Bld_Type == "Office/Warehouse")
            {
            this.currentState = "stateB"
            //***swatchCOL="blue";
            }
    ]]>
</fx:Script>

选项b)使用AS3:

var fillColor:SolidColor;
if(data.Bld_Type == "Office")
{
fillColor = new SolidColor(0xYOUR_HEX_COLOR);
bgFill.fill = fillColor;
}    
else if(data.Bld_Type == "Office/Warehouse")
{
fillColor = new SolidColor(0xYOUR_OTHER_HEX_COLOR);
bgFill.fill = fillColor;
}


<s:Group left="10" right="10" top="10" bottom="10">
    <s:Rect id="bgFill" width="25" height="25">
        <s:stroke>
            <s:SolidColorStroke color="green" weight="2"/>                        
        </s:stroke>
        <s:fill>
            <s:SolidColor color="#ffffff"/> <!-- #ffffff is an example of a default color -->
        </s:fill>
    </s:Rect>
</s:Group>