This application demonstrates the problem with the default ItemRenderer behavior when using view states within an item renderer. In the example on the left the added content disappears as soon as you mouse out of the expanded cell. The example on the right overrides getCurrentRendererState to prevent that. Right-click the app to view source or download an FXP file here.
One of the things that makes Flex so attractive is the extensive set of default behaviors provided in its many controls. Sometimes, however, that default behavior is not what you want and you have to work with Flex to get your desired behavior. I ran into a case of that recently when working with a DataGroup.
I created an ItemRenderer which contained a button the user could click to expand the cell to provide some additional information about the product in that cell. That worked fine but I discovered that the DataGrid row collapsed back to its original size as soon as I moved the mouse out of that cell. The problem is that the Flex ItemRenderer base class includes some default behaviors for mouse over events (among others). For mouse over, the current cell is highlighted and, of course, for mouse out the highlight is removed. This was fine but, in my case, the problem was that when I moused out, the ItemRenderer reverted to its normal state—which was the state without the added content. Here’s my original item renderer:
<?xml version="1.0" encoding="utf-8"?> <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" autoDrawBackground="true"> <s:states> <s:State name="normal" /> <s:State name="details" /> </s:states> <s:Rect height="100%" width="100%"> <s:stroke> <s:SolidColorStroke color="black"/> </s:stroke> </s:Rect> <s:VGroup paddingTop="10" paddingLeft="10" paddingBottom="10" paddingRight="10"> <s:Label text="Always Displayed" includeIn="normal, details"/> <s:Label text="{data}"/> <s:Group> <s:Button label="Add 2nd label" includeIn="normal" click="currentState='details'" /> <s:Button label="Remove 2nd label" includeIn="details" click="currentState='normal'"/> </s:Group> <s:Label text="Detailed description" includeIn="details"/> </s:VGroup> </s:ItemRenderer> |
There are two states, “normal” which is the original state without the “Detailed description” label, and “details” which does include this label. There is also a pair of buttons to allow the user to show and hide the detailed description label. Only one of those buttons is shown at any given time based on the value of currentState.
The solution I found was to override the ItemRenderer method getCurrentRendererState. I also added two states, including the hovered state which the Flex ItemRenderer base class implements:
<s:states> <s:State name="normal" /> <s:State name="hovered" /> <s:State name="details" /> <s:State name="hoveredDetails"/> </s:states> |
I modified my item renderer to apply the new states appropriately:
<s:VGroup paddingTop="10" paddingLeft="10" paddingBottom="10" paddingRight="10"> <s:Label text="Always Displayed" includeIn="normal, hovered, details, hoveredDetails"/> <s:Label text="{data}" includeIn="normal, hovered, details, hoveredDetails"/> <s:Group> <s:Button label="Add 2nd label" includeIn="normal, hovered" click="currentState='details'" /> <s:Button label="Remove 2nd label" includeIn="details, hoveredDetails" click="currentState='normal'"/> </s:Group> <s:Label text="Detailed description" includeIn="details, hoveredDetails"/> </s:VGroup> |
And I implemented my override of getCurrentRendererState:
protected override function getCurrentRendererState():String { var newRenderState:String = super.getCurrentRendererState(); if((currentState == "details" || currentState == "hoveredDetails") && newRenderState == "normal") { newRenderState = "details"; } if(currentState == "details" && newRenderState == "hovered") { newRenderState = "hoveredDetails"; } return newRenderState; } |
Here, I force the state to include the details even when the user mouses out and, in so doing, get the desired behavior.
Download sample project (FXP).
{ 2 comments }

