Accessing a List from its itemRenderer

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="http://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.

  • http://brickner.brixel.org Brixel

    Hello there. I’ve been searching for a solution to a similar problem. I have a custom MXML component, which is a tree. It has some public properties in it, etc. It points to an AS class as it’s itemRenderer.

    What is escaping me is the ability to target the public properties of that component from with in the AS class. I can get to the instance of the tree, but not the stuff inside the component container itself.

    Now calling something like “document.myCustomTreeComponent.myPublicProperty” will work, but I shouldn’t have to go all the way out and back in just to get what should be in my “parent” container, should I?

    Hoping for any help, thanks.

  • Paul

    thanks so much for posting this, you concisely made clear what the Flex manual fails to, helped me a lot

  • Mike

    Thanksfor this. But how do you access your item renderer's list from its parent??

  • Chad T Fraser

    you can reference the list from its renderer using this.parentDocument