2011-09-29 53 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时(通常都是这样)。 –