默认情况下,在Flex 中基于List 的控件都是使用dataProvider 中的元素的label 属性来做显示。在一些情况中,无论如何,dataProvideer 中都没有label 属性存在,这些情况就需要你来设定连接dataProvider 中的多个字段来实现一个显示值。这个labelFunction 属性允许用户定义自己的方法来呼叫dataProvider 中每个元素,然后对于每个元素返回显示值。如下范例,ComboBox 的labelFunction 属性包含了一个getFullName 函数的引用,这个函数连接了dataProvider 中单个元素的Name 和Value字段来返回一个全名的字串。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; [Bindable] private var dataProvider:ArrayCollection=new ArrayCollection([{id: 1, Name: "book", Value: "java"}, {id: 2, Name: "book", Value: "flex"}]); private function getFullName(item:Object):String { return item.Name + " " + item.Value; } ]]> </mx:Script> <mx:ComboBox id="cb" dataProvider="{dataProvider}" labelFunction="getFullName"/> </mx:Application>