2011-09-29 51 views
0

我正在使用Flex 4.0,Halo主題和我有源代碼的自定義組件。我想用參數調用組件來隱藏或顯示一些項目。該參數是動態的,意味着它是在mxml加載後的方法中設置的。如何在Flex中動態更新mxml屬性?

段:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    xmlns:ns1="de.aggro.components.*" 
    width="100%" 
    height="100%" 
    initialize="init();" 
> 

<mx:Script> 
    <![CDATA[ 
     [Bindable] 
     public var allowwindowmanipulation:Boolean = true; 

     internal function init():void { 
      allowwindowmanipulation = false; 
     } 
    ]]> 
</mx:Script> 
<mx:states> 
    <mx:State name="st"> 
     ... 
     <ns1:CollapsableTitleWindow width="956" height="395" x="294" y="484" id="wnd" title="title" allowClose="{allowwindowmanipulation}"> 
    </mx:State> 
</mx:states> 

</mx:Application> 

CollapsableTitleWindow代碼(片斷):

public class CollapsableTitleWindow extends Panel implements ICTWindow {  
    public var allowClose:Boolean = true; 

    function CollapsableTitleWindow(){ 
     super(); 
     addEventListener(FlexEvent.CREATION_COMPLETE, initializeHandler); 
    } 

    protected override function createChildren():void{ 
     super.createChildren();   

     //Create the Buttons 
     closeButton = new Sprite(); 

     if(allowClose==true){ 
      titleBar.addChild(closeButton); 
      drawCloseButton(true); 
     } 
    } 
} 

現在,當運行此代碼,給予的CollapsableTitleWindow構造函數的值是trueallowClose變量。所以當調用createChildren()時,該按鈕被繪製。

我該如何改變行爲?我不介意如果按鈕第一次被繪製,然後後來被刪除,如果需要的話,但我不知道我怎麼做這個綁定的參數值。

當我爲實例title屬性更改爲{allowClose}false布爾值被顯示,儘管可能是一個true在第一個通過。

回答

1

注:編輯,以反映反饋

由於您使用的Flex 4,你應該與皮膚內的皮膚和國家做到這一點。但是,如果你想通過代碼來做到這一點,嘗試這樣的事情:

protected var _allowClose:Boolean=true; 
protected var _allowCloseChanged::Boolean=true; 
protected var _closeButton:Sprite = new Sprite(); 


public function get allowClose():Boolean { 
    return _allowClose; 
} 

public function set allowClose(value:Boolean):void { 
    if (value != _allowClose) { 
     _allowClose=value; 
     _allowCloseChanged=true; 
     invalidateProperties(); 
    } 
} 

override protected function commitProperties():void { 
    if (_allowCloseChanged) { 
     if (_allowClosed) { 
      titlebar.addChild(_closeButton); 
      drawCloseButton(true); 
     } else { 
      titleBar.removeChild(_closeButton); 
     } 
     //you need to set this back false...sorry I left that out 
     _allowCloseChanged=false; 
    } 
    super.commitProperties(); 
} 

注:在這種情況下,您不再需要重寫createChildren

+0

我調查你的解決方案,因爲我們說話,似乎被正確調用,但我的其他代碼(我沒有包含在代碼片段中)應該發生在「drawCloseButton」代碼附近,目前正在失敗,所以我正在修復第一個.. – Tominator

+0

喜悅,它作品! (我不得不用自己的代碼來調試一下)。但是有兩件事情:commitProperties應該是「protected override」而不是「public」,並且在「if(_allowCloseChanged){」應該是「_allowCloseChanged = false;」謝謝! – Tominator

+0

對不起,早上寫得太早了:)。您必須將其更改爲(!_allowCloseChanged)的原因是因爲我忘記在commitProperties中將其設置爲false。只要_allowClose更改(因此是標誌),您就希望運行該代碼,而不是每次運行commitProperties時(通常都是這樣)。 –