package { import flash.display.DisplayObject; import flash.events.MouseEvent; import flash.text.TextField; import mx.controls.CheckBox; import mx.controls.DataGrid; publicclass CenteredCheckBoxHeaderRenderer extends CenteredCheckBox { publicvar stateHost:Object; publicvar stateProperty:String; override publicfunction set data(value:Object):void { selected = stateHost[stateProperty]; } // this function is defined by mx.controls.CheckBox // it is the default handler for its click event override protectedfunction clickHandler(event:MouseEvent):void { super.clickHandler(event); // this is the important line as it updates the external variable // we've designated to hold our state stateHost[stateProperty] = selected; } } }
// click events will still bubble privatefunction onCheckBoxHeaderClick(event:MouseEvent):void { // make sure click came from header if (event.target is CenteredCheckBoxHeaderRenderer) { // loop over data for each (var obj:Object in dg.dataProvider) { // update value based on CheckBox state obj.addToCart = CenteredCheckBoxHeaderRenderer(event.target).selected; // notify collection item was changed ListCollectionView(dg.dataProvider).itemUpdated(obj,"addToCart"); } } }