Adobe flex:希望DropDownList自定义渲染器以十六进制显示值并以十六进制显示所选值

时间:2012-09-28 04:11:03

标签: flex actionscript flex4 flash-builder

我有一些我希望以十进制存储的数据,但在s:DropDownList中以十六进制显示。我开发了自定义渲染器,下拉列表以十六进制显示数字,但是当选择数字时,它以十进制显示。我希望将数字存储为十进制,但始终以十六进制显示,包括下拉列表和选择时。我怎么能这样做?

自定义渲染器:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"        
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:s="library://ns.adobe.com/flex/spark">

    <s:Label text="{makeLabel(data)}"/>

    <fx:Script>
    <![CDATA[
        public function makeLabel(data:Object):String {
            var x:int = data as int;
            var ret:String = x.toString(16);
            if (ret.length < 2) {
                ret = '0' + ret;
            }
            return ret.toUpperCase() + 'H';
        }
    ]]>
    </fx:Script>
</s:ItemRenderer>

测试存根:

<?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" 
               minWidth="955" minHeight="600">

    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        [Bindable]
        public var dp:ArrayCollection = new ArrayCollection([9, 10, 11, 12]);
    ]]>
    </fx:Script>

    <s:DropDownList x="72" y="43" dataProvider="{dp}" itemRenderer="HexListRenderer"/>

</s:Application>

1 个答案:

答案 0 :(得分:2)

您可以使用DropDownList.labelFunction

<?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"
               minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
        import mx.collections.ArrayCollection;

        [Bindable]
        public var dp:ArrayCollection = new ArrayCollection([9, 10, 11, 12]);

        public function makeLabel(data:Object):String {
            var x:int = data as int;
            var ret:String = x.toString(16);
            if (ret.length < 2) {
                ret = '0' + ret;
            }
            return ret.toUpperCase() + 'H';
        }
    ]]>
    </fx:Script>

    <s:DropDownList x="72" y="43" labelFunction="makeLabel" dataProvider="{dp}" itemRenderer="HexListRenderer"/>

</s:Application>