2011-02-11 62 views
0

嘿那裏 - 我試圖根據dataprovider中找到的內容更改List組件的contentBackgroundColor。例如:更改contentBackgroundColor of List with itemRenderer

<s:ItemRenderer name="ir" 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    autoDrawBackground="true" 
    contentBackgroundColor="{(data.location == 'home')?0x000000:0x666666}"> 

不幸的是,這似乎被忽略,因爲列表只顯示了默認的白色背景。任何人都可以提出解決方案

回答

1

我會重寫組數據setter方法和設置樣式有,因爲你是保證抓住每一個變化的數據:

override public function set data(value:Object):void { 
    super.data = value; 
    this.setStyle("contentBackgroundColor", value.location == "home" ? 0x000000 : 0x666666); 
} 
+0

好的調用,但仍然被忽略。如果我在itemrenderer設置數據方法中追蹤(value.location),它會追蹤更多次,然後我在我的xml中有位置元素。實際上兩倍於+ 1。非常奇怪的行爲... – worked 2011-02-11 19:12:32

+0

嗯,如果將autoDrawBackground設置爲false會發生什麼?另外,另一種方法是將其設置爲false,並將Rect添加到項目渲染器的背景中,然後設置填充顏色。 – 2011-02-11 19:15:48

1

的ItemRenderer從集團擴展其不尊重contentBackgroundColor,而是通過它通過它的元素作爲一種繼承風格。

所以contentBackgroundColor的工作,但並不像你期待,如果你把那個不尊重contentBackgroundColor到您的渲染器的組件,然後將獲取的顏色,例如:

<s:List> 
    <s:dataProvider> 
     <s:ArrayList> 
      <fx:String>0</fx:String> 
     </s:ArrayList> 
    </s:dataProvider> 
    <s:itemRenderer> 
     <fx:Component> 
      <s:ItemRenderer contentBackgroundColor="red"> 
       <s:VGroup paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"> 
        <s:Label text="ItemRenderer {data}" /> 
        <s:ComboBox /> 
       </s:VGroup> 
      </s:ItemRenderer> 
     </fx:Component> 
    </s:itemRenderer> 
</s:List> 

正如指出的更早的時候,你可能會最好的重寫數據設置器,並從那裏改變背景Rect的顏色,例如:

<s:List> 
    <s:dataProvider> 
     <s:ArrayList> 
      <fx:String>0</fx:String> 
      <fx:String>1</fx:String> 
     </s:ArrayList> 
    </s:dataProvider> 
    <s:itemRenderer> 
     <fx:Component> 
      <s:ItemRenderer> 
       <fx:Script> 
        <![CDATA[ 
         override public function set data(value:Object):void { 
          super.data = value; 
          if (data == null) 
           return; 

          if (data == 1){ 
           c.color = 0xEEEEEE; 
          } else { 
           c.color = 0x666666; 
          } 
         } 
        ]]> 
       </fx:Script> 
       <s:Rect width="100%" height="100%"> 
        <s:fill> 
         <s:SolidColor id="c" /> 
        </s:fill> 
       </s:Rect> 
       <s:Label text="ItemRenderer {data}" /> 
      </s:ItemRenderer> 
     </fx:Component> 
    </s:itemRenderer> 
</s:List>