Macromedia flex 2 Manuale

Pagina di 254
Adding properties and methods to a component
135
When you define a property by using getter and setter methods so that the property is usable 
as the source for data binding, you include the 
[Bindable]
 metadata tag before the getter 
method, and optionally include the name of the event dispatched by the setter method when 
the property changes, as the following example shows:
package myComponents
{
    // as/myComponents/TextAreaFontControlBinding.as    
    import mx.controls.TextArea;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    
    public class TextAreaFontControlBinding extends TextArea 
    {
        public function TextAreaFontControlBinding() 
        {
            super();
            addEventListener("keyDown", myKeyDown);     
            addEventListener("creationComplete", myCreationComplete);       
        }       
        private var currentFontSize:Number;
        public var minFontSize:Number = 5;      
        // Define private variable for maxFontSize.
        public var _maxFontSize:Number = 15;
            
        // Define public getter method, mark the property
        // as usable for the source of data binding, 
        // and specify the name of the binding event.
        [Bindable("maxFontSizeChanged")]
        public function get maxFontSize():Number {
            return _maxFontSize;
        }
        // Define public setter method.
        public function set maxFontSize(value:Number):void {
            if (value <= 30) {
                _maxFontSize = value;
            } else _maxFontSize = 30;
            // Dispatch the event to trigger data binding. 
            dispatchEvent(new Event("maxFontSizeChanged"));
        }        
        
        private function myCreationComplete(eventObj:Event):void {           
            // Get current font size
             currentFontSize = getStyle('fontSize');
        }