Flex - Sending a parameter to a custom ItemRenderer?

后端 未结 10 1343
悲哀的现实
悲哀的现实 2020-12-24 07:37

What I am trying to accomplish to to get financial data in my Flex Datagrid to be color-coded--green if it\'s positive; red if it\'s negative. This would be fairly straight

相关标签:
10条回答
  • 2020-12-24 07:58

    You could create an 'AvailableFunding' static variable in the ItemRenderer and then set it in the parent document.

    public class PriceLabel extends Label {
        public static var availableFunding:int;
        ...
        ...
        SetStyle("color", (PriceLabel.availableFunding >= 0) ? NEGATIVE_COLOR : POSITIVE_COLOR);
    }
    

    In your parent document, set it when your text box gets updated

    PriceLabel.availableFunding = textBox.text;
    

    Obviously it'll be the same value for every ItemRenderer but it looks like that might be what you're doing anyway.

    0 讨论(0)
  • 2020-12-24 08:01

    You can access the value of the TextBox directly, if you need to, by using the static Application.application object, which is accessible from anywhere in your application.

    For example, if you wanted the renderers to be notified when the value of the TextInput control changes, you could do something like this (from within your ItemRenderer, and where myTextInput is the ID of the control defined in your main MXML class):

    <mx:Script>
        <![CDATA[
    
            import mx.core.Application;
    
            private function creationCompleteHandler(event:Event):void
            {
                Application.application.myTextInput.addEventListener(TextEvent.TEXT_INPUT, handleTextInput, false, 0, true);
            }
    
            private function handleTextInput(event:TextEvent):void
            {
                if (event.currentTarget.text == "some special value")
                {
                   // Take some action...
                }
            }
    
        ]]>
    </mx:Script>
    

    With this approach, each item-renderer object will be notified when the TextInput's text property changes, and you can take appropriate action based on the value of the control at that time. Notice as well that I've set the useWeakReference argument to true in this case, to make sure the listener assignments don't interfere unintentionally with garbage collection. Hope it helps!

    0 讨论(0)
  • 2020-12-24 08:02

    I vote up for cliff.meyers' answer.

    Here's another example on setting the properties of an itemRenderer from MXML by building a function that wraps a ClassFactory around the itemRenderer class and that injects the necessary properties.

    The static function:

    public static function createRendererWithProperties(renderer:Class,
    properties:Object ):IFactory {
      var factory:ClassFactory = new ClassFactory(renderer); 
      factory.properties = properties;
      return factory;
    }
    

    A simple example that adds a Tooltip to each item in a list:

    <mx:List dataProvider="{['Foo', 'Bar']}" itemRenderer="{createRendererWithProperties(Label, {toolTip: 'Hello'})}"/>
    

    Reference:
    http://cookbooks.adobe.com/post_Setting_the_properties_of_an_itemRenderer_from_MXM-5762.html

    0 讨论(0)
  • 2020-12-24 08:05

    You may want to look into ClassFactory from the Flex APIs:

    This allows you to set a prototype Object with arbitrary types / values each of which will be passed to the item renderer. From the sample:

    var productRenderer:ClassFactory = new ClassFactory(ProductRenderer);
    productRenderer.properties = { showProductImage: true };
    myList.itemRenderer = productRenderer;
    

    The above code assumed that "ProductRenderer" has a public property called "showProductImage" which will be set with a value of "true."

    0 讨论(0)
提交回复
热议问题