This was really throwing me off tonight. I could not figure out how to get at the List from its itemRenderer. this.parent… no this.owner… no
So I started The Search™, trying to hunt down The Answer™. This is one of those things that doesn’t just jump out of the manual at you. Luckily, Peter Ent is running an extremely informative series on itemRenders. He says to get at the List, we need to simply access the listData property. But there’s a catch:
Most controls such as Text, Label, Button, CheckBox, and so forth, implement IDropInListItemRenderer. Most containers, such as HBox, Canvas, etc. do not implement that interface. If you want to use listData in an itemRenderer that extends a Container you will have to implement IDropInListItemRenderer yourself – I’ll cover that in the next article.
Luckily for us, it is a simple process:
1 | <mx:hbox xmlns:mx="https://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer"></mx:hbox> |
We will need to implement the appropriate methods in the itemRenderer’s Script tag also:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import mx.controls.listClasses.BaseListData; private var _listData:BaseListData; private var list:List; public function get listData() : BaseListData { return _listData; } public function set listData( value:BaseListData ) : void { _listData = value; list = listData.owner as List list.addEventListener( ListEvent.CHANGE, onListChange, false, 0, true ) } |
In my case, I wanted to listen for changes so that I could set one of the itemRenderer sub-component’s properties whenever the list had changed. Now to access the List:
if ( list.selectedItem == data ) doSomeStuff();
Easy as that.
The Accessing a List from its itemRenderer by Joel Hooks, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 3.0 United States License.