2013-10-04 66 views
1

我有一個ItemRenderer與數據有一些標誌。我需要根據這些標誌更改ItemRenderer顏色。我怎樣才能做到這一點?這是我的IR:如何從模型標誌變化自動更新itemRenderer狀態

<?xml version="1.0" encoding="utf-8"?> 
<s:ItemRenderer 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="false" 
      x="{data.x}" y="{data.y}"> 

<s:states> 
    <s:State name="node"/> 
    <s:State name="nodeIn"/> 
    <s:State name="nodeOut"/> 
    <s:State name="nodeTrafficLight"/> 
    <s:State name="nodeIntersection"/> 
    <s:State name="nodeBlocked"/> 
</s:states> 

<s:Ellipse width="16" height="16" x="-8" y="-8"> 
    <s:stroke> 
     <s:SolidColorStroke weight="2" 
          color="#FFFF00" 
          color.nodeTrafficLight="#FF00FF" 
          color.nodeIntersection="#FF9900" 
          color.nodeBlocked="#000000" 
          color.nodeIn="#00FF00" 
          color.nodeOut="#FF0000"/> 
    </s:stroke> 
    <s:fill> 
     <s:SolidColor alpha=".5" 
         color="#FFFF00" 
         color.nodeTrafficLight="#FF00FF" 
         color.nodeIntersection="#FF9900" 
         color.nodeBlocked="#000000" 
         color.nodeIn="#00FF00" 
         color.nodeOut="#FF0000"/> 
    </s:fill> 
</s:Ellipse> 

<fx:Script> 
    <![CDATA[ 
     override protected function getCurrentRendererState():String 
     { 
      if (data.isBlocked) 
       return "nodeBlocked"; 

      if (data.isIn) 
       return "nodeIn"; 

      if (data.isOut) 
       return "nodeOut"; 

      if (data.isIntersection) 
       return "nodeIntersection"; 

      return "node"; 
     } 
    ]]> 
</fx:Script> 
</s:ItemRenderer> 

但我沒有得到這些狀態更新時,關於這些標誌(如data.isIn)的變化。我的模特有[Bindable]標籤。

回答

1

首先;我不認爲x和y值在渲染器中有影響。列表類將處理渲染器的位置。

這就是說,你的渲染器不響應dataChange事件;因此渲染器在數據更改時不會自行更新。添加dataChange事件處理程序:

<s:ItemRenderer 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="false" 
      dataChange="onDataChange(event)"> 


<fx:Script> 
    <![CDATA[ 
     protected function onDataChange(event:Event):void{ 
      invalidateRendererState(); 
     } 
    ]]> 
</fx:Script> 

的invalidateRendererState()方法應強制的commitProperties()重新運行;這將依次強制執行getCurrentRendererState()。

如果由於某種原因,onDataChange()事件在您更改dataProvider中的數據時未觸發;您必須使用dataProvider上的itemUpdated()或refresh()函數來「宣佈」您更改了數據。

+1

謝謝! 'dataChange + invalidateRendererState'做了魔術!而x,y有效果是的! – Fabricio