Flex align checkbox in datagrid Flex
I use an item renderer to display a checkbox in my datagrid like;
<mx:DataGridColumn headerText="Visible" dataField="visibleInd" width="48"
itemRenderer="mx.controls.CheckBox"
rendererIsEditor="true"
editorDataField="selected"
/>
And that works fine, but the checkbox is aligned left like;
How can I align it in the middle?
I have used;
<mx:DataGridColumn headerText="Visible" dataField="visibleInd" width="48"
editorDataField="selected"
>
<mx:itemRenderer>
<fx:Component>
<mx:Box width="100%" height="100%"
horizontalAlign="center" verticalAlign="middle">
开发者_运维百科 <mx:CheckBox selected="{data.visibleInd}" />
</mx:Box>
</fx:Component>
</mx:itemRenderer>
But in this case my code does align the checkbox in the middle, but does not save data in my dataprovider.
Am I missing something?
Instead of using <mx:Box />
, use <mx:Canvas />
or <s:Group />
(in Flex 4).
Also, set the horizontalCenter="0"
on the checkbox.
For example:
<mx:itemRenderer>
<mx:Component>
<mx:Canvas width="100%" height="100%">
<mx:CheckBox selected="{data.visibleInd}" horizontalCenter="0" />
</mx:Canvas>
</mx:Component>
</mx:itemRenderer>
Simply use DataGridColumn's textAlign
Style:
<mx:DataGridColumn headerText="Visible" textAlign="center">
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox selected="{data.visibleInd}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
I am pretty sure this will work by changing mx:itemRenderer
to mx:itemEditor
.
Kindly use the below hint to place the checkbox & image in the centre of the column.
<mx:CheckBox paddingLeft="20" />
<mx:Image horizontalAlign="center"/>
See:
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
override public function prepare(hasBeenRecycled:Boolean):void {
if(data != null){
chb.selected = data[column.dataField];
}
}
protected function chb_clickHandler(event:MouseEvent):void{
data[column.dataField] = !chb.selected;
}
]]>
</fx:Script>
<s:CheckBox id="chb" click="chb_clickHandler(event)" horizontalCenter="0" verticalCenter="0"/>
</s:GridItemRenderer>
try making checkbox width to 100%
精彩评论