2011-11-08 39 views
0

我有一個項目渲染以下狀態:的Flex 4項目渲染器的背景顏色 - 狀態不工作

<s:states> 
    <s:State name="normal"/> 
    <s:State name="hovered"/> 
    <s:State name="selected"/> 
</s:states> 

我想徘徊在選擇時更改的項目的背景顏色。我添加以下行項呈示開口標籤:盤旋或所選比默認項目渲染器行爲(灰色)等時

contentBackgroundColor.hovered="0xff0018" 
contentBackgroundColor.selected="0xffff11" 

什麼也沒有發生。如果我讓說增加圖像中的寬度:

width.hovered="100" 

的形象得到調整預期,我如何能實現我期待的效果?

編輯:

<?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" 
       xmlns:objects="objects.*" 
       creationComplete="itemrenderer1_creationCompleteHandler(event)" 
       width="100%" height="100%" 
       contentBackgroundColor.hover="0xff0018" focusColor="0xff00ff" contentBackgroundAlpha="0.8" 
       dataChange="refreshView()"> 

    <s:states> 
     <s:State name="normal"/> 
     <s:State name="hovered"/> 
     <s:State name="selected"/> 
    </s:states> 

    <fx:Declarations> 
     <objects:EmbededImages id="embImages"/> 
    </fx:Declarations> 

    <fx:Script> 
     <![CDATA[ 

      import events.MyEventDispatcher; 
      import mx.events.FlexEvent; 

      private function itemrenderer1_creationCompleteHandler(event:FlexEvent):void 
      { 
       //this.parentApplication.MyEventDispatcher.Dispatcher.addEventListener(CastUpdateEvent.UPDATED, refreshView); 
       refreshView(); 
      } 

      public function refreshView():void 
      { 
       var profileImage:File = File.userDirectory.resolvePath("Movie Manager Settings\\profiles\\" + [email protected] + ".jpg"); 
       if(profileImage.exists) 
       { 
        personImage.source = profileImage.nativePath as String; 
       } 
       else 
       { 
        personImage.source = embImages.portrait; 
       } 
      } 

     ]]> 
    </fx:Script> 

    <s:Image id="personImage" top="7" bottom="7" left="7" height="70" width="50"/> 
    <s:RichText left="70" height="100%" width="100" text="{[email protected]}" textAlign="left" verticalAlign="middle" paddingRight="5"/> 
    <s:RichText right="10" height="100%" width="100" text="{[email protected]haracter}" textAlign="left" verticalAlign="middle"/> 

</s:ItemRenderer> 

+0

contentBackgroundColor是一個樣式,寬度是一個屬性。我想知道這是否有所作爲?也就是說,你應該發佈完整的itemRenderer代碼。你到底在設置contentBackgroundColor的是什麼? – JeffryHouser

+0

我添加了完整的項目渲染器代碼,這些樣式在組件中完美工作,所以看起來很奇怪,但它在項目渲染器中不起作用。 – DominicM

回答

-1

這個作品

<s:GridItemRenderer currentState="normal" > 
<fx:Script> 
    <![CDATA[ 
     import mx.events.FlexEvent; 

     protected function state1_enterStateHandler(event:FlexEvent):void{ 
      bc.setStyle("backgroundColor",0xff0018); 
     } 

    ]]> 
</fx:Script> 
<s:states> 
    <s:State name="normal"/> 
    <s:State enterState="state1_enterStateHandler(event)" name="myState"/> 
    <s:State name="selected"/> 
</s:states> 

<s:BorderContainer id="bc" left="0" right="0" height="{this.height}"/> 
<s:Button click="this.currentState = 'prova'"/> 

+0

這確實會起作用,但我希望在某些時候將樣式移動到外部css文件中,您能否看到爲什麼我添加的代碼不起作用? – DominicM

+0

我希望我理解你的問題 您可以通過這種方式在CSS定義SYLE: .itemRendererRollover { 的backgroundColor:0xff0018 } 在你的項目渲染器時state1_enterStateHandler()方法中,只需要輸入bc.setStyle (「styleName」,「itemRendererRollover」) 或者,它也應該以這種方式移除方法state1_enterStateHandler()並在th bc組件的mxml聲明中定義「styleName.state」屬性: 讓我知道如果作品! – marcocb

1

在CSS將這個:

s|ItemRenderer:hovered { 
contentBackgroundColor: #ff0018; 
} 
1

你應該在CSS中使用:

.myItemRendererStyleName:selected #idOflabelInItemRenderer { contentBackgroundColor:#ff0000; }

和在MXML: <s:ItemRenderer styleName="myItemRendererStyleName" .... />

<s:Label id="idOflabelInItemRenderer" includeIn="normal,selected" />