如何在Flex中侦听由渲染器调度的自定义事件?

时间:2011-05-02 17:13:16

标签: flex renderer datagridcolumn

我遇到以下问题:我为DataGridColumn实现了自定义headRenderer。想法是在单击列标题时有一个下拉菜单。问题是我不知道如何添加侦听器或将变量传递给渲染器,因为渲染器类是由ClassFactory实例化的。

渲染器类:

<?xml version="1.0"?>
<!-- itemRenderers\dataGrid\myComponents\RendererDGHeader.mxml -->
<mx:HBox xmlns:mx="library://ns.adobe.com/flex/mx" 
 xmlns:fx="http://ns.adobe.com/mxml/2009" 
 width = "100%"
>
<fx:Script>


// ...

// Event handler for the Menu control's change event.
        private function menuHandler(evt:MenuEvent):void  {
            //Alert.show("Label: " + evt.item.@label, "Clicked menu item");

            if (evt.item.@eventName == "typeLat"){
             typeIcon.load("assets/type_location.jpg"); 
             evt.item.toggled=true;
            //variable.setColumnType(myLabel.text, "type");  // how can I pass "variable" to this renderer?                
            } else if (evt.item.@eventName == "typeLng"){
             typeIcon.load("assets/type_location.jpg"); 
             evt.item.toggled=true;
            } else if (evt.item.@eventName == "typeTime"){
             typeIcon.load("assets/type_time.png"); 
              evt.item.toggled=true;
            }

            else if (evt.item.@eventName == "similarity"){
               dispatchEvent(new SimilarityEvent("selectSimilarity", null, null)); // how to listen to this event?
            }
        }



</fx:Script>

 <fx:Declarations>
    <fx:XML id="myMenuData">
        <root>
            <menuitem label="Select type..." eventName="copy">
                <menuitem label="Other..." eventName="typeOther"/>
                <menuitem type="separator"/>
                <menuitem label="Order" type="check" eventName="typeOther"/>
                <menuitem label="Range" type="check" eventName="typeOther"/>
                <menuitem label="Date/Time" type="check" eventName="typeTime"/>
                <menuitem label="Latitude" type="check" eventName="typeLat"/>
                <menuitem label="Longitude" type="check" eventName="typeLng"/>
            </menuitem>
            <menuitem label="Define similarity..." eventName="similarity"/>
            <menuitem type="separator"/>
            <menuitem label="Filter objects..." eventName="similarity"/>
            <menuitem label="Order" eventName="similarity">
                <menuitem label="Ascending" eventName="typeOther"/>
                <menuitem label="Descending" eventName="typeOther"/>
            </menuitem>
            <menuitem type="separator"/>
            <menuitem label="Remove" eventName="paste"/>
        </root>
    </fx:XML>
</fx:Declarations>



<mx:Label id="myLabel" click="showMenu();"/>
<mx:Image height="20" id = "typeIcon"/>

</mx:HBox>

以下是我实例化渲染器的方法:

            var newColumn:DataGridColumn = new DataGridColumn(); 
            newColumn.dataField = attr;
            newColumn.headerText = attr;
            newColumn.sortable = false;
            newColumn.editable = false;
            newColumn.draggable = false;
            newColumn.headerRenderer = new ClassFactory(newviews.RendererDataHeader);
            cols.push(newColumn);

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

将事件监听器添加到控件(DataGrid)并使事件冒泡,它将被捕获。