<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Morphic Group &#187; itemrenderer</title>
	<atom:link href="http://www.themorphicgroup.com/blog/tag/itemrenderer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.themorphicgroup.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 22 Feb 2010 18:20:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Example of Feature Rich Item Renderers in Flex 4</title>
		<link>http://www.themorphicgroup.com/blog/2009/10/15/example-of-feature-rich-item-renderers-in-flex-4/</link>
		<comments>http://www.themorphicgroup.com/blog/2009/10/15/example-of-feature-rich-item-renderers-in-flex-4/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 15:27:15 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[itemrenderer]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://www.themorphicgroup.com/blog/?p=306</guid>
		<description><![CDATA[This is an example of creating feature rich item renderers in Flex 4. The example retrieves this blog's RSS feed and displays each entry in a Spark List using Item Renderers. This example will demonstrate: Skinning an item renderer and list States in an item renderer Pass callback function to item renderer Override ItemRenderer 'set [...]]]></description>
			<content:encoded><![CDATA[<p>
This is an example of creating feature rich item renderers in Flex 4. The example retrieves this blog's RSS feed and displays each entry in a Spark List using Item Renderers. This example will demonstrate:</p>
<ul style="list-style:square">
<li>Skinning an item renderer and list</li>
<li>States in an item renderer</li>
<li>Pass callback function to item renderer</li>
<li>Override ItemRenderer 'set data'  as a known ValueObject</li>
<li>Swap icons/images based on data values</li>
</ul>
<p>
	<a href="http://www.themorphicgroup.com/examples/Flex4ItemRenderer01/Flex4ItemRenderer01.html" target="_blank"><img src="http://www.themorphicgroup.com/blog/wp-content/uploads/2009/10/itemrenderer_flex4_01_post.png" width="444" height="175" border="none" alt="" /></a><br />
	<br />
	&nbsp;<a href="http://www.themorphicgroup.com/examples/Flex4ItemRenderer01/Flex4ItemRenderer01.html" target="_blank"><strong>View the Example</strong> with source view enabled</a>
</p>
<p>	This blog post will cover the main points of the example but not the entire code, so view the example's source view to see all the code in full.</p>
<h3>Getting Blog Entries From RSS</h3>
<p>
	I won't go into too much detail about how the data is retrieved, view the source for all the details. The RSSService class makes a request to this blog's RSS url. That XML responce is turned into an ArrayCollection of BlogEntryVO value objects. The result ArrayCollection is set as the data provider for the Spark List.
</p>
<h3>Skinning the Spark List</h3>
<p>
	<strong>Spark List:</strong><br />
	The first step in skinning the Spark List is to define the 'skinClass':
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>List id=<span style="color: #990000;">&quot;blogList&quot;</span>
	skinClass=<span style="color: #990000;">&quot;com.themorphicgroup.skins.list.ListSkin&quot;</span>
	<span style="color: #004993;">change</span>=<span style="color: #990000;">&quot;blogList_selectionHandler(event)&quot;</span>
	caretChange=<span style="color: #990000;">&quot;blogList_selectionHandler(event)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
&nbsp;</pre>
<p>
	<strong>Skinning Spark List:</strong><br />
	Lets look at the 'com.themorphicgroup.skins.list.ListSkin' List skin class. The most important parts of this skin is the 'Scroller' and 'DataGroup' it surrounds.<br />
	The DataGroup is a simple container class in Flex 4 for holding data items and item renderers. The Scroller enables the DataGroup items to be scrolled. Full List skin code:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>SparkSkin xmlns<span style="color: #000000; font-weight: bold;">:</span>fx=<span style="color: #990000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
			 xmlns<span style="color: #000000; font-weight: bold;">:</span>s=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
			 xmlns<span style="color: #000000; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/halo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;</span>fx<span style="color: #000000; font-weight: bold;">:</span>Metadata<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000;">&#91;</span>HostComponent<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;spark.components.List&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>fx<span style="color: #000000; font-weight: bold;">:</span>Metadata<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>states<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;normal&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;over&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>states<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;!--</span> <span style="color: #004993;">background</span> <span style="color: #000000; font-weight: bold;">--&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>Rect <span style="color: #004993;">top</span>=<span style="color: #990000;">&quot;0&quot;</span> <span style="color: #004993;">right</span>=<span style="color: #990000;">&quot;0&quot;</span> <span style="color: #004993;">bottom</span>=<span style="color: #990000;">&quot;0&quot;</span> <span style="color: #004993;">left</span>=<span style="color: #990000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>stroke<span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>SolidColorStroke <span style="color: #004993;">color</span>=<span style="color: #990000;">&quot;0x999999&quot;</span> weight=<span style="color: #990000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>stroke<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>fill<span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>SolidColor <span style="color: #004993;">color</span>=<span style="color: #990000;">&quot;0xffffff&quot;</span> <span style="color: #004993;">color</span>.over=<span style="color: #990000;">&quot;0xf8f8f8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>fill<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>Rect<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;!--</span> scroller <span style="color: #000000; font-weight: bold;">--&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>Scroller id=<span style="color: #990000;">&quot;scroller&quot;</span>
				<span style="color: #004993;">left</span>=<span style="color: #990000;">&quot;0&quot;</span>
				<span style="color: #004993;">top</span>=<span style="color: #990000;">&quot;0&quot;</span>
				<span style="color: #004993;">right</span>=<span style="color: #990000;">&quot;0&quot;</span>
				<span style="color: #004993;">bottom</span>=<span style="color: #990000;">&quot;0&quot;</span>
				horizontalScrollPolicy=<span style="color: #990000;">&quot;off&quot;</span>
				minViewportInset=<span style="color: #990000;">&quot;1&quot;</span>
				focusEnabled=<span style="color: #990000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;!--</span> container <span style="color: #0033ff; font-weight: bold;">for</span> data<span style="color: #000000; font-weight: bold;">--&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>DataGroup id=<span style="color: #990000;">&quot;dataGroup&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>layout<span style="color: #000000; font-weight: bold;">&gt;</span>
				<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>VerticalLayout gap=<span style="color: #990000;">&quot;0&quot;</span> horizontalAlign=<span style="color: #990000;">&quot;contentJustify&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>layout<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>DataGroup<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>Scroller<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>SparkSkin<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;</pre>
<p>
	<strong>Skinning Spark List and global vertical scrollers:</strong><br />
	The last thing we will look at in skinning the List is the skin for the Scroller (VScroll). The skin classes for the scroller are in com.themorphicgroup.skins.scroller.vertical<br />
	To define the 'VerticalScrollbar' Skin to all Spark VScrollBar we do that in the css file:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
s<span style="color: #000000; font-weight: bold;">|</span>VScrollBar <span style="color: #000000;">&#123;</span>
	skinClass<span style="color: #000000; font-weight: bold;">:</span> ClassReference<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;com.themorphicgroup.skins.scroller.vertical.VerticalScrollbar&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<h3>Define an ItemRenderer for the Spark List</h3>
<p>
	In creationComplete we will create the item renderer for the Spark List, we will also define any needed properties.<br />
	In this case we want to pass in a callback funtion for when the delete icon is clicked in an item renderer.
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">//create itemrenderer</span>
<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> application_creationCompleteHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span>FlexEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//create itemrenderer</span>
	blogListItemRenderer = <span style="color: #0033ff; font-weight: bold;">new</span> ClassFactory<span style="color: #000000;">&#40;</span>BlogEntryItemRenderer<span style="color: #000000;">&#41;</span>;
	<span style="color: #009900;">//define properties (callback for item delete)</span>
	blogListItemRenderer.properties = <span style="color: #000000;">&#123;</span>deleteHandlerFunction<span style="color: #000000; font-weight: bold;">:</span>itemDeleteClick_handler<span style="color: #000000;">&#125;</span>;
	<span style="color: #009900;">//set itemrenderer to List</span>
	blogList.itemRenderer = blogListItemRenderer;
	.... <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//callback function for delete click</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> itemDeleteClick_handler<span style="color: #000000;">&#40;</span>blogEntryItem<span style="color: #000000; font-weight: bold;">:</span>BlogEntryVO<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	...
&nbsp;</pre>
<p>
Inside the Item Renderer when the delete icon is clicked the callback function is passed the current item renderer's data (a value object):
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> deleteHandlerFunction<span style="color: #000000; font-weight: bold;">:</span><a href="http://www.google.com/search?q=function%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:function.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color: #004993;">Function</span></a>;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> trash_btn_clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//callback function pass BlogEntryVO value object</span>
	deleteHandlerFunction<span style="color: #000000;">&#40;</span>blogEntry<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<h3>The ItemRenderer</h3>
<p>
	<strong>Setting data in the ItemRenderer: </strong><br />
	In the 'BlogEntryItemRenderer' we will override the the set data function and set a BlogEntryVO with the data of the item renderer:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> blogEntry<span style="color: #000000; font-weight: bold;">:</span>BlogEntryVO
&nbsp;
override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> <span style="color: #004993;">data</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color: #004993;">Object</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">super</span>.<span style="color: #004993;">data</span> = <span style="color: #004993;">value</span>;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">is</span> BlogEntryVO<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		blogEntry = BlogEntryVO<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>
	<strong>Displaying data in the ItemRenderer: </strong><br />
	To display the data values in the itemrenderer we will override updateDisplayList and set values to Labels etc:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> updateDisplayList<span style="color: #000000;">&#40;</span>unscaledWidth<span style="color: #000000; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color: #004993;">Number</span></a>, unscaledHeight<span style="color: #000000; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">super</span>.updateDisplayList<span style="color: #000000;">&#40;</span>unscaledWidth,unscaledHeight<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>title_lbl<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		title_lbl.<span style="color: #004993;">text</span> = blogEntry.title;
		...
&nbsp;</pre>
<h3>Skinning and States in the ItemRenderer</h3>
<p>
	To make the item renderer more attractive and interactive we will create some states. In Flex 4 you can specify not only the name of a state but a name that will define a group of states:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>states<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;normal&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;hovered&quot;</span> stateGroups=<span style="color: #990000;">&quot;[hoveredStates]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;selected&quot;</span> stateGroups=<span style="color: #990000;">&quot;[selectedStates]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;normalAndShowsCaret&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;hoveredAndShowsCaret&quot;</span> stateGroups=<span style="color: #990000;">&quot;[hoveredStates]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;selectedAndShowsCaret&quot;</span> stateGroups=<span style="color: #990000;">&quot;[selectedStates]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>states<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;</pre>
<p>
	Having the state and stateGroups names means we can define properties for those states. For example with can set the alpha of the background color as <code>alpha='0'</code> and them the when we are in one of the hovered states as <code>alpha.hoveredStates='.1'</code>:
</p>
<pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>Rect <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>fill<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>SolidColor <span style="color: #004993;">color</span>=<span style="color: #990000;">&quot;0x6699cc&quot;</span> <span style="color: #004993;">alpha</span>=<span style="color: #990000;">&quot;0&quot;</span> <span style="color: #004993;">alpha</span>.hoveredStates=<span style="color: #990000;">&quot;.1&quot;</span> <span style="color: #004993;">alpha</span>.selectedStates=<span style="color: #990000;">&quot;.25&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>fill<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>s<span style="color: #000000; font-weight: bold;">:</span>Rect<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>s<span style="color: #000000; font-weight: bold;">:</span>Label id=<span style="color: #990000;">&quot;title_lbl&quot;</span>
		 maxDisplayedLines=<span style="color: #990000;">&quot;1&quot;</span>
		 styleName=<span style="color: #990000;">&quot;myriad&quot;</span>
		 <span style="color: #004993;">left</span>=<span style="color: #990000;">&quot;10&quot;</span>
		 <span style="color: #004993;">top</span>=<span style="color: #990000;">&quot;10&quot;</span>
		 <span style="color: #004993;">right</span>=<span style="color: #990000;">&quot;26&quot;</span>
		 textDecoration.hoveredStates=<span style="color: #990000;">&quot;underline&quot;</span>
		 fontWeight.selectedStates=<span style="color: #990000;">&quot;bold&quot;</span>
		 <span style="color: #004993;">color</span>=<span style="color: #990000;">&quot;#000000&quot;</span>
		 <span style="color: #004993;">fontSize</span>=<span style="color: #990000;">&quot;14&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span>
&nbsp;</pre>
<h3>Summary</h3>
<p>
	This blog post does not cover every bit of code in the example so <a href="http://www.themorphicgroup.com/examples/Flex4ItemRenderer01/Flex4ItemRenderer01.html" target="_blank"><strong>View the Example</strong> with source view enabled</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.themorphicgroup.com/blog/2009/10/15/example-of-feature-rich-item-renderers-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Example of Feature Rich Dynamic Item Renderers in Flex 3</title>
		<link>http://www.themorphicgroup.com/blog/2009/02/20/example-of-feature-rich-dynamic-item-renderers-in-flex-3/</link>
		<comments>http://www.themorphicgroup.com/blog/2009/02/20/example-of-feature-rich-dynamic-item-renderers-in-flex-3/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 17:43:37 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[itemrenderer]]></category>

		<guid isPermaLink="false">http://www.themorphicgroup.com/blog/?p=62</guid>
		<description><![CDATA[In the last Flex 3 rich feature item renderer example we looked at item renderers that had some nice visual and functional features, in this example we will take it to the next step and introduce some dynamic abilities to the item renderers. By using dynamic item renderers you can create more flexible &#38; reusable [...]]]></description>
			<content:encoded><![CDATA[<p>
	In the <a href="http://www.themorphicgroup.com/blog/2008/09/15/example-of-feature-rich-itemrenderers-in-flex/">last Flex 3 rich feature item renderer example</a> we looked at item renderers that had some nice visual and functional features, in this example we will take it to the next step and introduce some dynamic abilities to the item renderers. </p>
<p>    By using dynamic item renderers you can create more flexible &amp; reusable itemrenderers, you can also do neat stuff like pass callback functions to handle events in the itemrenderer.</p>
<p>
	Here are some of the things this dynamic item renderer example will demonstrate:
</p>
<ul>
<li>Reuse the same item renderer for a DataGrid and a List, each with a different data provider</li>
<li>Pass delete/click function into item renderer</li>
<li>Dynamically change icons</li>
<li>Dynamically change the label function</li>
<li>Set label title style</li>
</ul>
<p>  <a href="http://www.leavethatthingalone.com/examples/flex/itemrenderer02"><img src="http://www.leavethatthingalone.com/examples/flex/itemrenderer02/itemrenderer_02_post.png" width="200" height="105" alt="" border="0" /></a><br />
	<br />
    <a href="http://www.leavethatthingalone.com/examples/flex/itemrenderer02"><strong>View Example</strong> (right click for source)</a>
</p>
<p>In this post we won't go over every detail of the code, so check out the source in the example. </p>
<p>To start of with the basics, here is how to setup a  itemrenderer using  ClassFactory:</p>
<pre>
private var personItemRendererFactory:ClassFactory;
private function onInitialize():void {
	personItemRendererFactory = new ClassFactory(MyItemRenderer);
	peopleColumn.itemRenderer = personItemRendererFactory;
}
...
&lt;mx:DataGrid id=&quot;peopleDataGrid&quot; dataProvider=&quot;{peopleDataProvider}&quot;&gt;
	&lt;mx:columns&gt;
		&lt;mx:DataGridColumn id="peopleColumn" /&gt;
	&lt;/mx:columns&gt;
&lt;/mx:DataGrid&gt;
</pre>
<p>The example above doesn't really do much more than setting in itemrenderer in the DataGrid tag itself would, so lets set a property of the itemrenderer:</p>
<pre>
private function onInitialize():void {
	personItemRendererFactory = new ClassFactory(MyItemRenderer);
	personItemRendererFactory.properties = {doStuff: true};
	peopleColumn.itemRenderer = personItemRendererFactory;
}
</pre>
<p>
In the above example we are now creating an itemrenderer and setting the 'doStuff' property using the ClassFactory properties object. Using the class factory properties we can set many things at once including functions. Let's say we want  to listen for a click event inside the itemrenderer we can pass in a callback function that gets called from the item renderer when clicked:
</p>
<pre>
private function onInitialize():void {
	personItemRendererFactory = new ClassFactory(MyItemRenderer);
	personItemRendererFactory.properties = {clickFunction: itemrendererClick_handler};
	peopleColumn.itemRenderer = personItemRendererFactory;
}

private function itemrendererClick_handler(evt:MouseEvent):void {
	Alert.show('clicked');
}

/* this in item renderer */
//inside the itemrenderer we need to define the click function
public var clickFunction:Function;

//and then for this example lets call the clickFunction when the canvas is clicked
&lt;mx:Canvas click=&quot;clickFunction(event)&quot;...
</pre>
<p>
	The above example allows a function to set on the itemrenderer. When the itemrenderer is clicked the event is handled in the same component that the holds DataGrid, this can be very convenient.</p>
<p>When using the properties of ClassFactory there is a something to be aware of; your itemrenderer may require a property to be set, but the ClassFactory and properties will not enforce that property to be required. For example if a click handler function is required in the ItemRenderer but not set in the properties there will be a runtime error when the ItemRenderer is clicked.</p>
<p>
	View the example and right click for source to see<br />
	more details on how to do things like dynamically change icons and label functions.<br />
	<a href="http://www.leavethatthingalone.com/examples/flex/itemrenderer02"><strong>View Example</strong> (right click for source)</a>
</p>
<p>This example is not intended to be a best practices for the most efficient item renderers, for further reading: <a href="http://www.adobe.com/devnet/flex/articles/itemrenderers_pt5_print.html">Efficient Item Renderers</a>,  <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_3.html">Advanced visual components</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themorphicgroup.com/blog/2009/02/20/example-of-feature-rich-dynamic-item-renderers-in-flex-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Example of Feature Rich ItemRenderers in Flex</title>
		<link>http://www.themorphicgroup.com/blog/2008/09/15/example-of-feature-rich-itemrenderers-in-flex/</link>
		<comments>http://www.themorphicgroup.com/blog/2008/09/15/example-of-feature-rich-itemrenderers-in-flex/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 22:00:39 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[itemrenderer]]></category>

		<guid isPermaLink="false">http://www.themorphicgroup.com/blog/?p=51</guid>
		<description><![CDATA[In response to some requests I got from friends wanting some more real world ItemRenderer examples, here is one that approaches the item renderer from a graphical approach and brings together some different techniques to create a nicely featured ItemRenderer. Here is what this example DataGrid/ItemRender demonstrates: MXML based ItemRenderer Override ItemRenderer 'set data' have [...]]]></description>
			<content:encoded><![CDATA[<p>
In response to some requests I got from friends wanting some more real world ItemRenderer examples, here is one that approaches the item renderer from a graphical approach and brings together some different techniques to create a nicely featured ItemRenderer.
</p>
<p>
Here is what this example DataGrid/ItemRender demonstrates:
</p>
<ul>
<li>MXML based ItemRenderer</li>
<li>Override ItemRenderer 'set data' have data as a known ValueObject</li>
<li>Swap icons/images based on data values</li>
<li>Highlight selected ItemRenderer</li>
<li>Enable ItemRenderer to dispatch an event to remove a person from the grid</li>
</ul>
<div style="center;">
<a href="http://www.leavethatthingalone.com/examples/flex/itemrenderer01" target="_blank"><br />
<img src="http://www.leavethatthingalone.com/examples/flex/itemrenderer01/itemrenderer_01_post.png" width="369" height="142" border="0" /><br />
<strong>View the Example with source enabled</strong><br />
</a>
</div>
<p>
    <strong>DataProvider and ValueObjects</strong><br />
    The first thing we'll look at in the example is the data and data provider for the Grid. Keep in mind this is just a quick demo way to load sample data, the goal is to create an ArrayCollection of  Value Objects. The sample XML data is loaded via an HTTPService. When the result comes back from the service we cast the result as an ArrayCollection, we then loop through each item in that result ArrayCollection. Each item is an Object containing the 'person' data defined in the XML. For this example we want to place each person data in a person ValueObject (VO). This VO provides a known data type that we will use as the 'data' in the ItemRenderer. Often when using AMF taking to ColdFusion/BlazeDS/Java/Etc the VO will be aliased to a data type on the server, so this example simulates the DataGrid provider being an ArrayCollection of VOs.<br />
	<br />
    This result function populates the DataProvider with our Person VOs:
</p>
<pre>
private function httpResult_handler(evt:ResultEvent):void {
	if (evt.result.people.person) {
		//result
		var resultAC:ArrayCollection = evt.result.people.person as ArrayCollection;
		//loop through each item in data
		for (var i:int=0;i&lt;resultAC.length;i++) {
			//create person VO
			var person:Person = new Person();
			//add data
			person.fill(resultAC[i]);
			//add person to main data provider
			peopleDataProvider.addItem(person);
		}
	}
}
</pre>
<p>
<strong>ItemRenderer and Override of 'set data'</strong><br />
Next we'll look at how the data is dealt with inside the ItemRenderer. The DataGrid's only column is set to have an ItemRenderer. When the DataProvider is populated the DataGrid is going to set the 'data' property on each row's ItemRenderer with the the associated VO in the DataProvider ArrayCollection. We want to make sure that the data inside the ItemRenderer is actually a Person VO object (not a generic Object), so we need to override the 'set data' function. When we override this function we are now able to cast the data value as a Person VO. This will allow us to refer to data inside the ItemRenderer as that VO with known attributes rather than a generic Object that it would otherwise be:
</p>
<pre>
[Bindable]private var person:Person;
override public function set data(value:Object):void {
	super.data = value;
	if (value is Person) {
		person = Person(value);
	}
}
</pre>
<p>
When overriding the set data function make sure to remember set 'super.data=value'. The ItemRenderer still needs to know the value of the data otherwise it will be treated like an empty row in the DataGrid.
</p>
<p>
<strong>Swap ItemRenderer Images Based on Data </strong><br />
As a nice UI feature let's display a male/female icon based on the person's gender. This is easy to do, we know the data will be a Person VO. So we can have the Image component ask for the correct image based on the 'person.gender' value:
</p>
<pre>
&lt;mx:Image id="gender_img" source="{getGenderImg(person.gender)}"...
</pre>
<p>
Since the person variable is Bindable if there is a change the 'getGenderImg' function will be asked to give the image its source:
</p>
<pre>
private function getGenderImg(gender:String):Class {
	if (gender == Person.GENDER_FEMALE) {
		return imgUserFemale;
	} else {
		return imgUserMale;
	}
}
</pre>
<p>
<strong>Highlight Selected ItemRenderer</strong><br />
Next let's add some nice functionality to make the ItemRenderer stand out a little more when it is selected. To do this we need to override another function.  This time we're going to override the updateDisplayList function, this will allow us to set some styles and graphical elements to the ItemRenderer is if is the selected itemRenderer. Using 'ListBase' we are able to find out if the 'owner' (the DataGrid) has this data selected:
</p>
<pre>
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
	super.updateDisplayList(unscaledWidth, unscaledHeight);
	if(ListBase(owner).isItemSelected(data)) {
		name_lbl.setStyle("styleName","pnameselected");
		selected_img.visible = true;
	} else {
		name_lbl.setStyle("styleName","pname");
		selected_img.visible = false;
	}
}
</pre>
<p>
<strong>Enable ItemRenderer to dispatch a remove person event</strong><br />
Lastly we'd like the trash can icon to remove a the person when clicked. There are other (maybe more preferable, but that's for a different example) methods, but for this example let's have the ItemRenderer dispatch an event and have a listener set to catch that event. On the click event of the trash can icon there is an event dispatched:
</p>
<pre>
public static const DELETE_PERSON_EVENT:String = "deletePerson";
private function deleteClick_handler():void {
	var dEvt:Event = new Event(DELETE_PERSON_EVENT,true);
	this.dispatchEvent(dEvt);
}
</pre>
<p>
This 'deletePerson' event is dispatched by the ItemRenderer and importantly has 'bubbles' set to true. This bubbling enables the parent component to simply add a listener for the event:
</p>
<pre>
myDG.addEventListener(PersonItemRenderer.DELETE_PERSON_EVENT,deletePerson_handler);
private function deletePerson_handler(evt:Event):void {
	if (myDG.selectedIndex != -1)
		peopleDataProvider.removeItemAt(myDG.selectedIndex);
}
</pre>
<p>
That's it for this example. The next example will build on this but go the route of a dynamic ItemRenderer, while they a little bit more complicated can be very flexible allowing for dynamic properties to be set on the itemRenderers.<br />
<br />
<a href="http://www.leavethatthingalone.com/examples/flex/itemrenderer01" target="_blank"><strong>View the Example</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themorphicgroup.com/blog/2008/09/15/example-of-feature-rich-itemrenderers-in-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
