Macromedia flex 2 Manual

Page of 254
Adding custom properties and methods to a component
95
You might use this new method with the 
click
 event of a 
Button
 control to change the 
display from long names to short names, as the following example shows:
<?xml version="1.0"?>
<!-- mxmlAdvanced/MainPropWithMethod.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*">
    <MyComp:StateComboBoxPropMethod id="myCB" shortNames="false"/>
    <mx:Button label="Use Short Names" click="myCB.setShortName(true);"/>
    
</mx:Application>
Defining properties in MXML
In MXML, you can use an MXML tag to define a property of any type, as long as the type 
refers to an ActionScript class name. For example, you can use the 
<mx:String>
<mx:Number>
, and 
<mx:Boolean>
 tags to define properties in your MXML components that 
take String, Number, or Boolean values, respectively. When using one of these tags, you must 
specify an 
id
, which becomes the property name.
Optionally, you can specify an initial value in the body of the tag, or you can use the 
source
 
property to specify the contents of an external URL or file as the initial property value. If you 
use the 
source
 property, the body of the tag must be empty. The initial value can be static 
data or a binding expression.
The following examples show initial properties set as static data and binding expressions; 
values are set in the tag bodies and in the 
source
 properties:
<!-- Boolean property examples: -->
<mx:Boolean id="myBooleanProperty">true</mx:Boolean>
<mx:Boolean id="passwordStatus">{passwordExpired}</mx:Boolean>
<!-- Number property examples: -->
<mx:Number id="myNumberProperty">15</mx:Number>
<mx:Number id="minutes">{numHours * 60}</mx:Number>
<!-- String property examples: -->
<mx:String id="myStringProperty">Welcome, {CustomerName}.</mx:String>
<mx:String id="myStringProperty1" source="./file"/>
All properties defined by using the 
<mx:String>
<mx:Number>
, and 
<mx:Boolean>
 tags are 
public. This means that the component user can access these properties.