简体   繁体   English

Flex中方法的数据绑定

[英]Data binding from a method in Flex

I have the following mxml (Omitted some parts for brevity purpose): 我有以下mxml(为简洁起见,省略了一些部分):

<fx:Script>
    [Bindable]
    private var _dataSet:IDataSet;

    public function set dataSet(value:IDataSet)
    {
      _dataSet = value;
    }

    private function getColorItem(itemName:*):String
    {
      if (itemName == "Research")  return "#31e5fc";
      ...
      else {
        trace("Unknown item : " + itemName);
        return #ffffff;
      }
    }  
</fx:Script>
<spark:RichText color="{this.getColorItem(this._dataSet.name)}" ... />

Does anyone knows why the color is not being applied? 有谁知道为什么不应用颜色? If I change the binding to the method, for a constant String (ie #31e5fc), it works allright? 如果我更改对方法的绑定,对于常量字符串(即#31e5fc),它可以正常工作吗?

I need the color to change according to the data received by this component. 我需要根据该组件接收到的数据来更改颜色。 Any help on where I am doing a mistake is much appreciated. 非常感谢我在哪里做错了。

You've got a couple of odd things here. 您在这里有一些奇怪的事情。 First, your getColorItem function should return a uint rather than a String. 首先,您的getColorItem函数应返回uint而不是String。 Second, why not just call getColorItem from your dataSet setter? 其次,为什么不从您的dataSet setter调用getColorItem? Something like this: 像这样:

public function set dataSet(value:IDataSet)
{
    _dataSet = value;
    myRichText.setStyle("color", getColorItem(_dataSet.name);
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM