8
votes

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;

alt text

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?

6
We've got some good answers for you below, any of them working for you? If so, why no love? :)Wade Mueller

6 Answers

16
votes

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>
5
votes

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>
0
votes

I am pretty sure this will work by changing mx:itemRenderer to mx:itemEditor.

0
votes

Kindly use the below hint to place the checkbox & image in the centre of the column.

<mx:CheckBox paddingLeft="20" />
<mx:Image horizontalAlign="center"/>
0
votes

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>
-1
votes

try making checkbox width to 100%