Flex3:自定义项目渲染器不侦听父级调度的事件 [英] Flex3: Custom Item Renderer does not listen to events dispatched by parent

查看:27
本文介绍了Flex3:自定义项目渲染器不侦听父级调度的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 ItemRenderer 的列表.ItemRenderer 包含一个复选框和一个标签.带有列表的组件有一个全选"复选框.当全选"复选框被选中时,它会调度一个事件,每个项目都应该侦听该事件以选择自己的复选框.每个item的creationComplete上都添加了eventlistener,当勾选了'select all'复选框时事件被正确调度,但是自定义ItemRenderer中的listener不监听.

I have a List with a custom ItemRenderer. The ItemRenderer contains a Checkbox and a Label. The component with the List has a 'select all' checkbox. When the 'select all' checkbox is checked, it dispatches an event that each item should listen to in order to select its own checkbox. The eventlistener is added on creationComplete of each item, and the event is dispatched correctly when the 'select all' checkbox is selected, but the listener in the custom ItemRenderer does not listen.

如何让 ItemRenderer 侦听在其父级中调度的事件?

How do I make the ItemRenderer listen to an event that is dispatched in its parent??

我将添加一些代码示例来阐明:

I'll add some code examples to clarify:

------- container ----------
<mx:VBox>
   <mx:Script>
      <![CDATA[
         public var user1 = new User(1, "Jack");
         public var user2 = new User(2, "John");
         public var user3 = new User(3, "Mary");

         [Bindable]
         public var users:ArrayCollection = new ArrayCollection([user1], [user2], [user3]);

         public static const SELECT_ALL_USERS:String = "selectAllUsers";

         private function selectAllChangeHandler():void
         {
            if (selectAll.selected)
               dispatchEvent(new Event(SELECT_ALL_USERS,true));
         }
      ]]>
   </mx:Script>
   <mx:CheckBox id="selectAll" change="{selectAllChangeHandler()}" />
   <mx:List dataProvider="{users}" itemRenderer="myRenderer" />
</mx:VBox>


------- renderer ----------
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox creationComplete="{init()}">
   <mx:Script>
      <![CDATA[
         private function init():void
         {
            addEventListener (Container.SELECT_ALL, selectAllHandler, false, 0, true);
         }

         private function selectAllHandler():void
         {
            checkbox.selected=true;
         }

         private function selected(id:int):Boolean
         {
             return id==1 || id==3;
         }
      ]]>
   </mx:Script>

   <mx:CheckBox id="checkbox" selected="{selected(data.id)}" />
   <mx:Label text="{data.name}" />
</mx:HBox>

请注意,用户 ArrayCollection 或其包含的用户对象无法更改,因为我稍后需要这些值.所以当selectAll"被点击时,列表中的每个复选框也应该被选中.

Please note that the users ArrayCollection or its containing user objects cannot be changed because I need the values later on. So when 'selectAll' is clicked, each checkbox in the list should also be checked.

推荐答案

您的自定义 ItemRenderer 不应向其父级注册事件侦听器,而应使用您的全选"复选框,即

Your custom ItemRenderers should not register an event listener with their parent, but with your "select all" checkbox, i.e.

theCheckbox.addEventListener(YourEvent.YOUR_EVENT, itemRendererSelectAllHandler);

如果失败,您能否发布添加事件侦听器并从复选框调度事件的代码?

Failing that, can you post your code which adds the event listener and which dispatches the event form the checkbox?

这是您的错误:在渲染器的 init() 中,您需要将事件侦听器添加到调度事件的容器中,而不是添加到渲染器中.所以把它变成一个

Here's your bug: In renderer's init(), you need to add an event listener not to the renderer, but to the container which dispatches the event. So make that a

container.addEventListener(Container.SELECT_ALL_USERS, selectAllHandler, false, 0, true);

这篇关于Flex3:自定义项目渲染器不侦听父级调度的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆