User Guide

96 Creating Advanced MXML Components
The following example modifies the example in “Defining properties and methods in
ActionScript” on page 93 to define the
shortNames property by using an MXML tag, rather
than an ActionScript variable definition:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/StateComboBoxPropMXML.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="setNameLength();">
<!-- Control display of state names. -->
<mx:Boolean id="shortNames">true</mx:Boolean>
<mx:Script>
<![CDATA[
// Define private variables.
private var stateArrayShort:Array = ["AK", "AL"];
private var stateArrayLong:Array = ["Arkansas", "Alaska"];
// Define listener method.
public function setNameLength():void {
if (shortNames) {
dataProvider=stateArrayShort; }
else {
dataProvider=stateArrayLong; }
}
]]>
</mx:Script>
</mx:ComboBox>
In the preceding example, you implement the StateComboBox.mxml file by using the
<mx:Boolean> tag to add a new property, shortNames, with a default value of true. This
property controls whether the ComboBox control displays state names that use a two-letter
format, or the entire state name.
Defining properties by using setters and getters
You can define properties for your MXML components by using setter and getter methods.
The advantage of getters and setters is that they isolate the variable from direct public access
so that you can perform the following tasks:
Inspect and validate any data written to the property on a write
Trigger events that are associated with the property when the property changes
Calculate a return value on a read