2011-03-18 91 views
1

我有一個提示字符串顯示在我的組合框 - 這需要顯示在斜體。當用戶從列表中進行任何選擇時 - 我需要更改顯示內容的樣式。如何在運行時更改組合框的樣式屬性 - textInputStyleName?

我的CSS文件:

.promptStyle 
{ 
    fontStyle: italic; 
} 
ComboBox.withPrompt 
{ 
    color: #FF0000; 
    fontWeight: normal; 
    textInputStyleName: promptStyle; 
} 
.regularStyle 
{ 
    fontStyle: normal; 
} 
ComboBox.withoutPrompt 
{ 
    color: black; 
    fontWeight: normal; 
    textInputStyleName: regularStyle; 
} 

我的MXML文件:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
      minWidth="955" minHeight="600" initialize="init()"> 

<mx:Script> 
    <![CDATA[ 
     [Bindable] 
     private var content:Array=new Array("Red", "Blue", "Green"); 

     private function init():void { 
      StyleManager.loadStyleDeclarations("combos/combo_style.swf"); 
     } 

     private function changeStyle():void { 
      var index:int = promptBox.selectedIndex; 
      if(index != -1) 
       promptBox.setStyle("styleName","withoutPrompt"); 
     } 
    ]]> 
</mx:Script> 

<mx:ComboBox id="promptBox" prompt="Select a color" dataProvider="{content}" 
     styleName="withPrompt" change="changeStyle()"/> 
</mx:Application> 

我能看到的風格變化發生,因爲顏色的變化;但特定於textInputStyleName的更改未得到應用。任何幫助,將不勝感激。

+0

不是我的專業領域。它看起來並不像你有這個設置正確的,但。您不希望將styleName更改爲字符串。你想改變它的CSS參考。嘗試從setStyle方法中刪除引號:promptBox.setStyle(「styleName」,withoutPrompt); – JeffryHouser 2011-03-18 13:13:23

回答

1

您應該將樣式分配給內部TextInput子組件,但爲此您必須派生自己的PromptingComboBox以訪問受保護的textInput屬性。

我覺得下面的類確實基本上你想要什麼,應該給你一個想法:

public class PromptingComboBox extends ComboBox implements IFactory 
{ 
    private var _dropDown: List = null; 

    public function PromptingComboBox() 
    { 
     super.dropdownFactory = this; 
    } 

    public function newInstance(): * 
    { 
     _dropDown = new List(); 
     _dropDown.addEventListener(ListEvent.CHANGE, onChangeDropDownList); 
     return _dropDown; 
    } 

    override protected function createChildren():void 
    { 
     super.createChildren(); 
     this.textInput.setStyle("fontStyle", "italic"); 
    } 

    private function onChangeDropDownList(event: Event): void 
    { 
     this.textInput.setStyle("fontStyle", ""); 
    } 
} 
0

謝謝:)我能得到它的子類組合框如你所說的工作。更新我的CSS中的textInputStyleName將是一個更清潔的解決方案,因爲這是一個巨大的現有應用程序,現在我必須進入幾個MXML並更改控件來使用自定義控件 - im猜測這是flex中的錯誤?

無論如何,感謝您的幫助!