Macromedia flex 2 Manuale

Pagina di 254
94
Creating Advanced MXML Components
The following MXML application file uses the 
<MyComp:StateComboBoxPropAS>
 tag to 
configure the control to display long state names:
<?xml version="1.0"?>
<!-- mxmlAdvanced/MainPropAS.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*">
    <MyComp:StateComboBoxPropAS shortNames="true"/>
    
</mx:Application>
The following example modifies the component to add a method that lets you change the 
display of the state name at run time. This public method takes a single argument that 
specifies the value of the 
shortNames
 property:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/StateComboBoxPropMethod.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="setNameLength();">
    <mx:Script>
        <![CDATA[
            // Define public variables.
            public var shortNames:Boolean = true;
                        
            // Define private variables. 
            private var stateArrayShort:Array = ["AK", "AL"];
            private var stateArrayLong:Array = ["Arkansas", "Alaska"];
            public function setNameLength():void {
                if (shortNames) {
                    this.dataProvider=stateArrayShort; }
                else {
                    this.dataProvider=stateArrayLong; }
            }                                           
                        
            public function setShortName(val:Boolean):void {
                shortNames=val;
                if (val) {
                    dataProvider=stateArrayShort; }
                else {
                    dataProvider=stateArrayLong; }
            }                                                                   
        ]]>
    </mx:Script>
</mx:ComboBox>