2010-09-07 129 views
1

我试图将一些属性传递给我在Flash Builder 4中创建的组件。在我的示例中,我想通过“label”属性更新标签Button的属性。将属性传递给Flash Builder 4中的自定义组件

任何帮助将不胜感激。提前致谢。

// MyApp.mxml 
<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication 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:local="*"> 
    <fx:Script> 
     <![CDATA[ 
      protected function buttonText():void 
      { 
       myButton.label = 'Clicked!'; 
      } 
     ]]> 
    </fx:Script> 
<local:MyComp id="myButton" label="My Button" click="buttonText()"/> 
</s:WindowedApplication> 

// MyComp.mxml 
<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" 
     width="400" height="300"> 
    <s:Button/> 
</s:Group> 

回答

2
<fx:Script> 
    <![CDATA[ 
     private var _label:String; 

     public function get label() : String { 
      return _label; 
     } 
     public function set label(value:String) : void { 
      _label = value; 
      myButton.label = value; 
     } 

     protected function buttonText():void 
     { 
      myButton.label = 'Clicked!'; 
     } 
    ]]> 
</fx:Script> 

这将创建控件的标签属性和myButton.label的标签属性之间的默认绑定。你也可以在标签属性的getter上使用[Bindable]元标签。无论哪种方式,您只需设置您的组件的标签属性,myButton标签的值将反映新值。不管怎样,您只需设置组件的标签属性,并且myButton标签的值将反映新值。

+0

非常感谢,这工作。 – Reado 2010-09-07 14:51:57

相关问题