+展开-ActionScript
package oreilly.cookbook {
import flash.display.DisplayObject;
import mx.events.FlexEvent;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.UIComponent;
public class SevenSixRenderer extends UIComponent implements IDropInListItemRenderer, IListItemRenderer {
private var _data:Object;
private var img:DisplayObject;
private var _listData:BaseListData;
private var _imgClass:Class;
[Bindable("dataChange")]
public function get data():Object {return _data;}
public function set data(value:Object):void {
_data = value;
if(_data.imgClass != null) {
_imgClass =_data.imgClass;
}
invalidateProperties(); // invalidate properties so that we'recertainthatthey'll be updated.
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
[Bindable("dataChange")]
public function get listData():BaseListData { return _listData; }
public function set listData(value:BaseListData):void { _listData = value; }
override protected function commitProperties():void {
super.commitProperties();
// sometimes the listdata of the renderer can be null, in which case we
//certainly don't
// want to throw runtime errors
if (listData) {
// remove the old child if we have one
if (img) {
removeChild(img);
}
if (_imgClass == null ) {
var _imgClass:Class = UIComponent(owner).document[ listData.label];
}
img = new _imgClass();
addChild(img);
}
}
/* create the image instance now that we know what it is */
override protected function measure():void {
super.measure();
if (img) {
measuredHeight = img.height;
measuredWidth = img.width;
}
}
/* make sure the image is positioned correctly */
override protected function updateDisplayList(w:Number,h:Number):void{
super.updateDisplayList(w, h);
if (img) { img.x = (w - img.width) / 2;
}
}
}
}
+展开-XML
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="700"
height="300">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Embed(source="../assets/foo.jpeg")]
private var img:Class;
[Embed(source="../assets/bar.jpeg")]
public var img2:Class;
//just a generic collection to store some plain old info
[Bindable]
private var genericCollectionOne:ArrayCollection = new ArrayCollection([{name:"josh noble"
, age:30, appearance:"Somewhat wild"},{name:"Abey George", age:32, appearance:"Pretty tight", imgClass:img},{name:"Todd Anderson", age:31,appearance:"Intimidating"},{name:"Ryan
Taylor", age:25, appearance:"Boyishly Handsome", imgClass:img},{name:"Steve Weiss", age:36, appearance:"George Clooney-ish"}]);
// for our itemRenderer we use the call into this method if the imgClass
//property is null
private function getImage(o:Object, c:DataGridColumn):String { return "img2";}
]]>
</mx:Script>
<mx:DataGrid dataProvider="{genericCollectionOne}">
<mx:columns>
<mx:DataGridColumn dataField="age"/>
<mx:DataGridColumn dataField="name"/>
<mx:DataGridColumn dataField="appearance"/>
<mx:DataGridColumn
itemRenderer="oreilly.cookbook.SevenSixRenderer"
labelFunction="getImage " dataField="imgClass"/>
</mx:columns>
</mx:DataGrid>
</mx:HBox>