我正在使用此页面顶部代码提供的自定义 DataGrid 子类:
http://blogs.adobe.com/aharui/2008/02/checkbox_selection_in_datagrid.html
我有效地为它提供了从 VB.NET Web 服务中提取的两列 DataRows 的 ArrayCollection。其中一列称为“isSelected”,设置为“True”或“False”,它是唯一由复选框表示的列。一切都很顺利,除了我希望代码能够进入并选中和取消选中复选框 - 至少取决于 isSelected 的值是什么。我的代码可以很好地从这些复选框中读取所有内容,用户可以很好地设置它们;只是 DataGrid 组件非常隐蔽并且使用起来很混乱,所以我还没有找到一种方法来让代码本身将特定的复选框设置为选中或取消选中。
你怎么做到这一点?谢谢!
编辑:目前渲染器的代码是这样的:
package
{
import flash.display.DisplayObject;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.ListBase;
/**
* The Renderer.
*/
public class CheckBoxRenderer extends CheckBox
{
private var _data:*;
private var selectedSet:Boolean;
override public function set data(value:Object):void {
var newSelected:*;
//so we don't have to rewrite the label stuff
super.data=value;
_data = value;
if (listData && listData is DataGridListData && DataGridListData(listData).dataField != null)
{
newSelected = _data[DataGridListData(listData).dataField];
}
else if (listData)
{
if (selectedField)
newSelected = _data[selectedField];
}
else
{
newSelected = _data;
}
if (newSelected !== undefined)
{
/*if (newSelected is XMLList) {
newSelected = newSelected[0];
selected = (newSelected == 'true' ? true : false);
} else {
selected = newSelected as Boolean;
}*/
selected = (newSelected == "True" ? true : false);
}
}
public function CheckBoxRenderer()
{
focusEnabled = false;
}
/* override public function set data(value:Object):void
{
super.data = value;
invalidateProperties();
}*/
override protected function commitProperties():void
{
super.commitProperties();
if (owner is ListBase)
selected = ListBase(owner).isItemSelected(data);
}
/* eat keyboard events, the underlying list will handle them */
override protected function keyDownHandler(event:KeyboardEvent):void
{
}
/* eat keyboard events, the underlying list will handle them */
override protected function keyUpHandler(event:KeyboardEvent):void
{
}
/* eat mouse events, the underlying list will handle them */
override protected function clickHandler(event:MouseEvent):void
{
}
/* center the checkbox if we're in a datagrid */
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
if (listData is DataGridListData)
{
var n:int = numChildren;
for (var i:int = 0; i < n; i++)
{
var c:DisplayObject = getChildAt(i);
if (!(c is TextField))
{
c.x = (w - c.width) / 2;
c.y = 0;
}
}
}
}
}
}
复选框的选定字段正在设置为正确的东西,但在选定的设置为true时,它在视觉上显示。我还缺少什么?