Adam Harte
Adam Harte

Reputation: 10510

Binding to an specific property of objects in an array

I am using Flex to create a small form. All I have at the moment is a List component, that I want to populate with a list of font names.

I am getting the fonts using Font.enumerateFonts(true);. This returns an array of flash.text.Font objects.

The Font objects have a fontName property that is a String of that fonts name.

My problem is that I can't figure out how to bind the List's dataProvider to the fontName property of each of the Font objects in the Array.

Is there a way to do this just with binding? and not creating a new array of Strings by looping through the Font objects?

Upvotes: 0

Views: 551

Answers (1)

Christian Nunciato
Christian Nunciato

Reputation: 10409

You're probably looking for the labelField property of the List control. Here's a working example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInitialize()">

    <mx:Script>
        <![CDATA[

            import mx.collections.ArrayCollection;

            [Bindable]
            private var fonts:ArrayCollection;

            private function onInitialize():void
            {
                fonts = new ArrayCollection(Font.enumerateFonts(true));
            }

        ]]>
    </mx:Script>

    <mx:List dataProvider="{fonts}" labelField="fontName" />

</mx:Application>

Also note that I'm using an ArrayCollection for the binding (as opposed to an Array), since Arrays don't support binding in the way you're expecting.

Hope that helps! Any questions, feel free to post back.

Upvotes: 3

Related Questions