Macromedia flex 2 Manual

Página de 254
84
Creating Simple MXML Components
The root tag of an MXML component cannot contain an 
id
 property. Therefore, if you refer 
to the object defined by the root tag in the body of the component, you must use the 
this
 
keyword, as the following example shows: 
<?xml version="1.0"?>
<!-- mxml/myComponents/StateComboBoxThis.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" 
    close="handleCloseEvent(event);">
    <mx:Script>
        <![CDATA[
            import flash.events.Event;
            // Define a property to hold the current index.
            public var stateIndex:Number;               
            private function handleCloseEvent(eventObj:Event):void {
                stateIndex = this.selectedIndex;
            }   
        ]]>
    </mx:Script>
    <mx:dataProvider>   
        <mx:String>AK</mx:String>
        <mx:String>AL</mx:String>
    </mx:dataProvider>
</mx:ComboBox>
This example defines an event listener for the ComboBox control that updates the 
stateIndex
 property when the ComboBox control closes. 
Applying styles to your custom 
component
Along with skins, styles define the look and feel (appearance) of your Flex applications. You 
can use styles to change the appearance of a single component, or apply them across all 
components. 
When working with custom components, you have several options for how you use styles. 
You can define your custom components so that they contain no style information at all. That 
design allows the application developer who is using your component to apply styles to match 
the rest of their application. For example, if you define a custom component to display text, 
the application developer can style it to ensure that the font, font size, and font style of your 
component match the rest of the application.