User Guide

Table Of Contents
32 Chapter 2: Creating Basic Components in Flash MX 2004
Add the ChangeEvent metadata keyword to the getter, and a dispatchEvent() method call
inside the setter. When the property’s value is set, the component dispatches the
change event.
Since the getter is bound to the ChangeEvent
change, the binding subsystem knows what to
listen for when that property changes.
At the top of the class file, you must also add the Event metadata keyword to identify
change as
an event that this component emits.
The following ActionScript class file shows how to bind a property to an event:
[Event("change")]
class BindChar extends mx.core.UIComponent {
static var symbolName:String="BindChar";
static var symbolOwner:Object = BindChar;
var className:String = "BindChar";
function BindChar() {
}
// This example uses a TextField to store and display the character.
var myCharField:TextField;
function init() {
super.init();
tabEnabled = true;
invalidate();
}
function keyDown(evt:Object):Void {
//Triggers the setter on every keystroke.
char = String.fromCharCode(evt.ascii);
}
[ChangeEvent("change")]
public function get char():String {
return myCharField.text;
}
public function set char(c:String) {
myCharField.text = c;
dispatchEvent({ type: "change" });
}
}
The following MXML file binds the component’s char property to the TextArea control’s
text property:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" >
<BindChar id="myComp1" char="F" />
<mx:TextArea id="ta1" text="{myComp1.char}" />
</mx:Application>