Flex Images: Visible Property Funkiness in itemRenderer

In the Flex app that I’m currently working on, I have a DataGrid that uses an itemRenderer to display an image. The image is a ‘delete’ button that allows the user to delete the item. Fairly straightforward, and the code below was working fine…

<mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="">
	<mx:itemRenderer>
		<mx:Component>
			<mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" />
		</mx:Component>
	</mx:itemRenderer>
</mx:DataGridColumn>

… until the customer called and said that the user should not be allowed to delete a record if that record meets a particular condition. OK. I think I can do that. I made some modifications and now had the following code:

<mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="" dataField="CAN_DELETE">
	<mx:itemRenderer>
		<mx:Component>
			<mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" visible="{data.CAN_DELETE == 1}" />
		</mx:Component>
	</mx:itemRenderer>
</mx:DataGridColumn>

I added a “dataField” property to the DataGridColumn, and added a “visible” property to the image itself, bound to the value of CAN_DELETE. Recompiled, and still saw that every record displayed the delete image. I wondered if my logic, as simple as it was, might have been off. just for kicks and giggles, I hard-coded the value “false” into the “visible” property of the image. Still, it displayed for all records. Now this was odd.

One of the big perks of my current job is that i get to work with Tariq Ahmed, Flex guru and all around good guy. While he wasn’t able to offer up a reason as to why this behavior was happening, he suggested throwing the image into an <mx:HBox> component. New code:

<mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="" dataField="CAN_DELETE">
	<mx:itemRenderer>
		<mx:Component>
			<mx:HBox>
				<mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" visible="{data.CAN_DELETE == 1}" />
			</mx:HBox>
		</mx:Component>
	</mx:itemRenderer>
</mx:DataGridColumn>

You’ll notice the only change is the addition of the <mx:HBox> around the <mx:Image>. For whatever reason, that worked perfectly. I don’t pretend to know why, but I’m curious as heck. Ff you’ve got any ideas, please feel free to comment and enlighten me.