1 Reply Latest reply on Dec 22, 2009 8:59 AM by David_F57

    Exposing properties in mxml components

    Handycam Level 1

      I am tring to figure out how to create a property in a component so that I can set said property in the instance via mxml.

       

      For example, I have an Group mxml component with a label and a text field.  I want to be able to set the text property of the label via the component defininition, as in something like:

       

      <comp:MyGroup itemLabel="Enter number of apples:" />

       

      I know I can do it with an ActionScript variable, such as:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
                 xmlns:s="library://ns.adobe.com/flex/spark" 
                 xmlns:mx="library://ns.adobe.com/flex/mx">
           <s:layout>
                <s:HorizontalLayout verticalAlign="middle"/>
           </s:layout>
           
           <fx:Script>
                <![CDATA[
                     [Bindable] public var itemLabel:String;
                ]]>
           </fx:Script>
           
                <s:Label text="{itemLabel}" styleName="formLabels" width="200"/>
                <s:TextInput id="wallLength" width="100" restrict="0-9\\." text="0" focusIn="{event.currentTarget.text=''}"  />
      </s:Group>
      
      

      Can't this be done in all mxml?  Or is it better to do it like I have it?

        • 1. Re: Exposing properties in mxml components
          David_F57 Level 5

          something like this

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

             xmlns:s="library://ns.adobe.com/flex/spark"

             xmlns:mx="library://ns.adobe.com/flex/mx" 

             xmlns:ns1="*">

           

          <fx:Script>

          <![CDATA[

          ]]>

          </fx:Script>

          <ns1:SpecialLabel x="219" y="229" mySpecial="hello" EnterStuff="moretyping"/>

           

          </s:Application>

          component mySpecial
          <?xml version="1.0" encoding="utf-8"?>
          <s:Label xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx" width="319">
          <fx:Declarations>
          <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <fx:Script>
          <![CDATA[
          private var _mySpecial:String;
          public var EnterStuff:String;
          public function set mySpecial(value:String): void
          {
          _mySpecial=value;
          }
          ]]>
          </fx:Script>
          </s:Label>
          Once you save the mxml component its properties become available in the property inspector as well as the code complete for the value declarations in the component.
          David