<?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>Silverlight &#124; WPF &#124; Microsoft.Net &#187; Visual State Manager</title>
	<atom:link href="http://joel.neubeck.net/tag/visual-state-manager/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Wed, 26 May 2010 18:43:15 +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>Templated Silverlight Rating Control</title>
		<link>http://joel.neubeck.net/2008/09/templated-silverlight-rating-control/</link>
		<comments>http://joel.neubeck.net/2008/09/templated-silverlight-rating-control/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 21:36:26 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[ContentControl]]></category>
		<category><![CDATA[ControlTemplate]]></category>
		<category><![CDATA[Rating Control]]></category>
		<category><![CDATA[Template Binding]]></category>
		<category><![CDATA[Visual State Manager]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/09/templated-silverlight-rating-control/</guid>
		<description><![CDATA[Anyone that has been working with the standard set of controls for Silverlight 2 Beta 2 has been exposed to the concept of template binding and how VisualStateManager (VSM) controls state.&#160;&#160; In this example I will build a templated control&#160; to learn how to use VSM, template binding and the ContentControl base class.&#160; The control [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone that has been working with the standard set of controls for Silverlight 2 Beta 2 has been exposed to the concept of template binding and how VisualStateManager (VSM) controls state.&#160;&#160; In this example I will build a templated control&#160; to learn how to use VSM, template binding and the ContentControl base class.&#160; </p>
<p>The control I choose to create is a Rating control .&#160; The goal is to display a series of Stars that both visualize an overall average score, and allows a user to submit their own rating.&#160; Here is what the control looks like.</p>
<p> <iframe src="/wp-content/uploads/2008/09/Rating/default.html" frameborder="0" width="300" height="200"></iframe>  <br /> <br />
<h2>RatingControl</h2>
<p>The control is comprised of two components: RatingControl and RatingButton. The RatingControl is derived from ContentControl and can either be given an explicit set of RatingButtons, or can automatically create them based on a user setting. Here are the properties I have chosen to expose in RatingContorl:</p>
<ul>
<li>Max (Int): The number of RatingButtons to display </li>
<li>Total (double): The number of votes that have been cast </li>
<li>Score (double): The average rating across all&#160; submissions </li>
<li>UseIndicators(bool): Determines if we will display indicators for what each star represents. </li>
</ul>
<p>There are two ways in which you can populate the rating control.&#160; The first is to set the Max property and allow the control to add the appropriate number of children.&#160; This works great if you don’t need to specify an individual Indicator (ToolTip) for each RatingButton.&#160; The second approach is to explicitly add RatingButtons to the content property of the RatingControl.&#160; Here is what that would look like in Xaml.</p>
<p> <a href="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-1.png"><img title="rating_1" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="143" alt="rating_1" src="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-1-thumb.png" width="450" align="left" border="0" /></a>   <br style="clear: both" />
<p>By choosing to have our RatingControl inherent from ContentControl it takes care of giving us the ability to set the children of our Content property.&#160; In this example I am assuming a FrameworkElement derived from Panel (Grid or StackPanel) is placed inside of Content.&#160; This makes it very easy to iterate through the panels children and identify each RatingButton.&#160; The following shows the code I used to make the decisions if I would create my own RatingButtons, or use those specified in the RatingControl.Content element.</p>
<p><a href="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-2.png"><img title="rating_2" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="335" alt="rating_2" src="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-2-thumb.png" width="450" align="left" border="0" /></a> </p>
<p>  <br style="clear: both" />
<p>Since I was allowing this control to be templated, I choose to do this setup in the controls Loaded event handler.&#160; This allows me to set the controls Content property equal to my manually created StackPanel, prior to the controls ApplyTemplate method being executed.&#160; This ensures that any special formatting around the panel is applied after the RatingButtons have been added.</p>
<h2>RatingButton</h2>
<p>To minimize the amount of code I needed to write for this control I decided to build my RatingButton control on top of the System.Windows.Controls.Primitives.ToggleButton.&#160; If you are not familiar with this control it is a very flexible Button that supports three states (Checked, Uncheked, Null).&#160; This is perfect for my example where I will need my star to be visualizes as either filled, empty or half filled star. </p>
<p>For all extensive purposes I left the ToggleButton alone and only overrode OnToggle, OnMouseEnter and OnMouseDown.&#160; Here is what that code looks like.</p>
<p><a href="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-3.png"><img title="rating_3" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="364" alt="rating_3" src="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-3-thumb.png" width="450" align="left" border="0" /></a> </p>
<p>  <br style="clear: both" />
<p>Since each RatingButton needs to behave as a group, I had to circumvent how the ToggleButton would change visual state, and allow RatingControl to manage this process.&#160; The first method UpdateChecked, simply iterates over each rating button, and based on the Score, determines which ones would be checked.&#160; For my visualization I decide that an average score &gt; N.3, but &lt; N.7 would visualize a half star, where as anything above or below would visualize a full or empty star.&#160; I allow the IsChecked property of ToggleButton to switch to the appropriate visual state (Checked, UnChecked or Intermediate).</p>
<p>In the event that a user is hovering over the rating control, I want to visualize a different color of filled star.&#160; Once again I need to allow the RatingControl to determine which RatingButtons need to react to the hover.&#160; The following demonstrates how I achieved this effect. </p>
<p><a href="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-4.png"><img title="rating_4" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="317" alt="rating_4" src="http://joel.neubeck.net/wp-content/uploads/2008/09/rating-4-thumb.png" width="450" align="left" border="0" /></a> </p>
<p>  <br style="clear: both" />
<p>Since once again we choose to circumvent how the ToggleButton switches state on hover, we must&#160; use VisualStateManger.GotToState to trigger the appropriate state of the RatingButton we are evaluating (MouseOver or Normal).&#160;&#160; </p>
<p>To&#160; customize the visualization of the this control you can either edit my default style in Blend 2.5, or you can clone it and make your own changes. The Control template is as simple as I could make it to allow users full control over how the RatingButton icons look, and how the states are visualized.&#160; </p>
<p>Code: <a onclick="javascript: pageTracker._trackPageview('/code/Rating.zip');" href="/wp-content/uploads/2008/09/Rating/Rating.zip">Rating.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/09/templated-silverlight-rating-control/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
