<?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>scorbs &#187; controlmodel</title>
	<atom:link href="http://scorbs.com/tag/controlmodel/feed/" rel="self" type="application/rss+xml" />
	<link>http://scorbs.com</link>
	<description>karen corby&#039;s fabulous blog</description>
	<lastBuildDate>Tue, 10 Jan 2012 19:07:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Mix 09: Building Microsoft Silverlight Controls</title>
		<link>http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/</link>
		<comments>http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 02:58:46 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[mix09]]></category>

		<guid isPermaLink="false">http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/</guid>
		<description><![CDATA[Thank you to everyone to who came to my talk at Mix09!&#160; I’m always a bit nervous having talks on the last day of a conference. (After all, you work really hard to pull a session together – and it &#8230; <a href="http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>Thank you to everyone to who came to my talk at Mix09!&#160; </p>
<p>I’m always a bit nervous having talks on the last day of a conference. (After all, you work really hard to pull a session together – and it can kinda be a bummer talking to mostly empty chairs.)&#160; But my room was full, and the audience’s vibe felt great from stage.&#160; :) </p>
<p>If you missed the session, you can watch the <a href="http://videos.visitmix.com/MIX09/T16F">video</a> here:</p>
<p> <a href="http://videos.visitmix.com/MIX09/T16F"><img src="http://farm4.static.flickr.com/3447/3378529580_bf0b8abb3b.jpg" /></a>
<p>Also, as promised, here are links to…</p>
<li><a href="http://scorbs.com/silverlight/mix09/T16F-Corby.zip">Sample source code</a> </li>
<li><a href="http://scorbs.com/silverlight/mix09/T16F-Corby.pptx">Deck</a>
<p>The final <a href="http://scorbs.com/silverlight/mix09/WishExplorer/">WishExplorer</a> demo uses <a href="http://www.wishpot.com">Wishpot</a>’s <a href="http://www.wishpot.com/help/developers.aspx">3rd party web service</a>.&#160; </p>
<p>   <a href="http://scorbs.com/silverlight/mix09/WishExplorer/"><img src="http://farm4.static.flickr.com/3067/3378538890_ca85250a83.jpg" /></a>
<p><a href="www.wishpot.com">Wishpot</a> is a great site for wishlisting – it’s aimed at avoiding “random” (note quotations) presents on your birthday and holidays. :)&#160; Anyways, in order to compile &amp; run the demo locally, you’ll need to request a developer key from them <a href="http://www.wishpot.com/help/developers.aspx">here</a>.</p>
<p>Also, here’s a link to two other demos, if you’d link to click around them:</p>
<p>   <a href="http://scorbs.com/silverlight/mix09/WishDetails/"><img src="http://farm4.static.flickr.com/3652/3377720397_d3f82464c6_m.jpg" /></a> <a href="http://scorbs.com/silverlight/mix09/WishControl/"><img src="http://farm4.static.flickr.com/3655/3377720311_7b6134c2aa_m.jpg" /></a>
<p>A few callouts:</p>
<ul>
<li>Thank you to Wishpot’s CTO <a href="http://www.lianza.org/">Tom Lianza</a> and&#160; Senior Designer John Hildenbiddle for all their help on the backend and visuals for the demos. </li>
<li><a href="http://blogs.msdn.com/devdave/">Dave</a> – your radial panel rocked it.&#160; Thanks! </li>
</ul>
<p>Any feedback you have on the session content or presentation is appreciated – I’m always trying to be a better speaker.&#160; :)</p>
<p>p.s.&#160; I’ve posted an update to my <a href="http://scorbs.com/2008/03/18/snippetmanager/">SnippetManager</a> – it now automatically copies the snippets to the clipboard.</p>
</p>
</li>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Source Code for Silverlight 2 Controls</title>
		<link>http://scorbs.com/2009/01/15/source-code-for-silverlight-2-controls/</link>
		<comments>http://scorbs.com/2009/01/15/source-code-for-silverlight-2-controls/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 19:53:35 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>

		<guid isPermaLink="false">http://scorbs.com/2009/01/15/source-code-for-silverlight-2-controls</guid>
		<description><![CDATA[For those of you who didn’t see, Seema Ramchandani (a fellow PM on the Silverlight team) blogged that we released source code for the Silverlight 2 controls. Check it out here!]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>For those of you who didn’t see, <a href="http://blogs.msdn.com/seema">Seema Ramchandani</a> (a fellow PM on the Silverlight team) blogged that we released source code for the Silverlight 2 controls.</p>
<p>Check it out <a href="http://blogs.msdn.com/seema/archive/2009/01/07/published-the-control-source-code-for-silverlight-2-runtime-sdk.aspx">here</a>!</p>
<p><a href="http://blogs.msdn.com/seema/archive/2009/01/07/published-the-control-source-code-for-silverlight-2-runtime-sdk.aspx"><img style="display: inline" title="control source VS explorer" alt="control source VS explorer" src="http://scorbs.com/wp-content/uploads/2009/01/controlsourcevsexplorer.jpg" width="114" height="240" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2009/01/15/source-code-for-silverlight-2-controls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PDC: Silverlight 2 Control Model Session (PC29)</title>
		<link>http://scorbs.com/2008/10/31/pdc-silverlight-2-control-model-session-pc29/</link>
		<comments>http://scorbs.com/2008/10/31/pdc-silverlight-2-control-model-session-pc29/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 23:25:28 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[pdc]]></category>
		<category><![CDATA[speaking]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/10/31/pdc-silverlight-2-control-model-session-pc29</guid>
		<description><![CDATA[Thank you to everyone who made it to my talk on Thursday! As promised, some links: Session deck Session samples Session recording Also, if you haven&#8217;t already, please take the time to fill out the online evaluation.&#160; Your comments help &#8230; <a href="http://scorbs.com/2008/10/31/pdc-silverlight-2-control-model-session-pc29/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>Thank you to everyone who made it to my talk on Thursday!</p>
<p>As promised, some links:</p>
<ul>
<li><a href="http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/PC29.pptx">Session deck</a>
<li><a href="http://scorbs.com/silverlight/pdc08/PC29demos.zip">Session samples</a>
<li><a href="http://channel9.msdn.com/pdc2008/PC29/">Session recording</a></li>
</ul>
<p>Also, if you haven&#8217;t already, please take the time to fill out the <a href="https://sessions.microsoftpdc.com/public/evals.aspx">online evaluation</a>.&nbsp; Your comments help me be a better speaker.&nbsp; :)</p>
<p>&nbsp;<a href="http://channel9.msdn.com/pdc2008/PC29/"><img height="322" alt="talk" src="http://scorbs.com/wp-content/uploads/2008/11/talk.jpg" width="640"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/10/31/pdc-silverlight-2-control-model-session-pc29/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Silverlight @ PDC!</title>
		<link>http://scorbs.com/2008/10/27/silverlight-pdc/</link>
		<comments>http://scorbs.com/2008/10/27/silverlight-pdc/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 17:05:56 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[spain (2008)]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[pdc]]></category>
		<category><![CDATA[speaking]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/10/27/silverlight-pdc</guid>
		<description><![CDATA[Here are some of the Silverlight-focused PDC sessions.&#160; &#160; Hope to see you there! Session Time Speaker Expression Blend: Tips and Tricks Mon 10/2712:45-1:30pm Pete Blois,Douglas Olson Silverlight, WPF, and the .NET Framework: Sharing Skills and Code Mon 10/273:30 &#8211; &#8230; <a href="http://scorbs.com/2008/10/27/silverlight-pdc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>Here are some of the Silverlight-focused PDC sessions.&nbsp; </p>
<p>&nbsp;<a href="http://scorbs.com/wp-content/uploads/2008/10/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="71" alt="image" src="http://scorbs.com/wp-content/uploads/2008/10/image-thumb.png" width="228" border="0"></a> </p>
<p>Hope to see you there!</p>
<table cellspacing="0" cellpadding="2" width="478" border="2">
<tbody>
<tr>
<td valign="top" width="263"><strong>Session</strong></td>
<td valign="top" width="101"><strong>Time</strong></td>
<td valign="top" width="110"><strong>Speaker</strong></td>
</tr>
<tr>
<td valign="top" width="262">Expression Blend: Tips and Tricks</td>
<td valign="top" width="101"><em>Mon 10/27<br />12:45-1:30pm</em></td>
<td valign="top" width="111"><em>Pete Blois,<br />Douglas Olson</em></td>
</tr>
<tr>
<td valign="top" width="262">Silverlight, WPF, and the .NET Framework: Sharing Skills and Code</td>
<td valign="top" width="101"><em>Mon 10/27<br />3:30 &#8211; 4:45PM</em></td>
<td valign="top" width="111"><em>Ian Ellison-Taylor</em></td>
</tr>
<tr>
<td valign="top" width="262">Silverlight: Building Business Focused Apps</td>
<td valign="top" width="101"><em>Tues 10/28<br />3:30 &#8211; 4:45pm </em></td>
<td valign="top" width="111"><em>Jamie Cool</em></td>
</tr>
<tr>
<td valign="top" width="262">Deep Dive: Building an Optimized, <br />Graphics-Intensive Application in Microsoft Silverlight</td>
<td valign="top" width="101"><em>Tues 10/28<br />5:15 &#8211; 6:30pm</em></td>
<td valign="top" width="111"><em>Seema<br />Ramchandani</em></td>
</tr>
<tr>
<td valign="top" width="262">Silverlight&nbsp; Controls Roadmap</td>
<td valign="top" width="101"><em>Wed10/29<br />12:00 &#8211; 12:45pm</em></td>
<td valign="top" width="111"><em>Shawn&nbsp; Burke</em></td>
</tr>
<tr>
<td valign="top" width="262">Inside the Olympics: An Architecture and Development Overivew</td>
<td valign="top" width="101"><em>Wed 10/29<br />1:15 &#8211; 2:30pm</em></td>
<td valign="top" width="111"><em>Eric Schmidt, <br />Jason Suess</em></td>
</tr>
<tr>
<td valign="top" width="262">Microsoft Silverlight 2: Control Model</td>
<td valign="top" width="102"><em>Thurs 10/30<br />10:15 &#8211; 11:30am</em></td>
<td valign="top" width="112"><em>Karen Corby</em></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/10/27/silverlight-pdc/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>An Early Look &#8211; VisualStateManager for WPF</title>
		<link>http://scorbs.com/2008/08/08/an-early-look-visualstatemanager-for-wpf/</link>
		<comments>http://scorbs.com/2008/08/08/an-early-look-visualstatemanager-for-wpf/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 22:58:11 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/08/08/an-early-look-visualstatemanager-for-wpf</guid>
		<description><![CDATA[John Gossman, one of the awesome Silverlight and WPF architects, just blogged a prototype of VisualStateManager for WPF.&#160; VisualStateManager will be added in to a future release of the .NET Framework.&#160; For those of you who want an early look, &#8230; <a href="http://scorbs.com/2008/08/08/an-early-look-visualstatemanager-for-wpf/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p><a href="http://blogs.msdn.com/johngossman">John Gossman</a>, one of the awesome <a href="http://silverlight.net/">Silverlight</a> and <a href="http://windowsclient.net/default.aspx">WPF</a> architects, just blogged a prototype of <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of">VisualStateManager</a> for WPF.&nbsp; </p>
<p>VisualStateManager will be added in to a future release of the .NET Framework.&nbsp; For those of you who want an early look, you can now play with John&#8217;s <a href="http://blogs.msdn.com/johngossman/archive/2008/08/08/visualstatemanager-for-desktop-wpf.aspx">VSM implementation</a>.&nbsp; (<u>This is for prototyping only</u> &#8211; it is not a supported feature yet.)&nbsp; He uses AttachedProperties to hook up VisualStateManager-enabled Templates to WPF controls. </p>
<p>You&#8217;ll also see my WeatherControl using VSM on WPF!</p>
<p><a href="http://blogs.msdn.com/johngossman/archive/2008/08/08/visualstatemanager-for-desktop-wpf.aspx"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="178" alt="weather" src="http://scorbs.com/wp-content/uploads/2008/08/weather.jpg" width="240" border="0"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/08/08/an-early-look-visualstatemanager-for-wpf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>FlickrViewr Updated for Beta2</title>
		<link>http://scorbs.com/2008/07/10/flickrviewr-updated-for-beta2/</link>
		<comments>http://scorbs.com/2008/07/10/flickrviewr-updated-for-beta2/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 01:03:26 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/07/10/flickrviewr-updated-for-beta2/</guid>
		<description><![CDATA[Just a note to say I&#8217;ve updated my FlickrViewr for Silverlight Beta2.&#160; (All of the controls now use VisualStateManager, too!) Enjoy! (See the original post for more info &#38; source code.)]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>Just a note to say I&#8217;ve updated my <a href="http://scorbs.com/2008/03/14/flickrviewr/">FlickrViewr</a> for Silverlight Beta2.&nbsp; (All of the controls now use <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">VisualStateManager</a>, too!)</p>
<p>Enjoy!</p>
<p><a href="http://scorbs.com/silverlight/flickr/flickrviewr/"><img height="213" src="http://farm3.static.flickr.com/2202/2332719642_ed297d7e4f.jpg?v=0" width="240"></a></p>
<p>(See the <a href="http://scorbs.com/2008/03/14/flickrviewr/">original post</a> for more info &amp; source code.)</p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/07/10/flickrviewr-updated-for-beta2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Parts &amp; States Model with VisualStateManager (Part 4 of 4)</title>
		<link>http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/</link>
		<comments>http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:42:23 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/</guid>
		<description><![CDATA[Here we are &#8211; the final post of our Parts &#38; States Model series for Silverlight 2 controls.&#160; Hope you&#8217;ve enjoyed the ride so far! Today, we&#8217;ll go through some general recommendations on how to use the Parts &#38; States &#8230; <a href="http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>Here we are &#8211; the final post of our Parts &amp; States Model series for Silverlight 2 controls.&nbsp; Hope you&#8217;ve enjoyed the ride so far!</p>
<p>Today, we&#8217;ll go through some general recommendations on how to use the Parts &amp; States model.&nbsp; We&#8217;ll also take a look ahead:&nbsp; VisualStateManager on Windows Presentation Foundation and future releases of Silverlight.</p>
<p>&nbsp;</p>
<p>(Series Link:&nbsp; <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">Part 1</a>, <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Part 2</a>, <a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Part 3</a>, <a href="http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/">Part 4</a>)</p>
<p>&nbsp;</p>
<p><strong><u>Parts &amp; States Model Recommendations</u></strong></p>
<p><em><strong>1.&nbsp; Use the Parts &amp; States Model with Custom Controls and UserControls</strong></em></p>
<p>Like we mentioned in the first post, the Parts &amp; States Model is just a pattern.&nbsp; It is not required by the runtime, and it&#8217;s perfectly fine to build controls without the Parts &amp; States Model and VisualStateManager.</p>
<p>However, we do think this is a good model.&nbsp; And Blend will only be able to support skinning of custom controls using the Parts &amp; States Model.</p>
<p>And while this series concentrated on VSM with Custom Controls, remember you can use it with UserControls as well!</p>
<p><em><strong></strong></em></p>
<p><em><strong>2.&nbsp; Custom VSM xmlns</strong></em></p>
<p>This one is less of a recommendation and more of a heads up.</p>
<p>Because of a known Silverlight 2 Beta 2 bug, you need to use a custom xmlns for VisualStateManager and its related classes.</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> xmlns:vsm=“clr-namespace:System.Windows;assembly=System.Windows”</pre>
</div>
</div>
<div>&nbsp;</div>
<div><em><strong>3.&nbsp; Naming conventions</strong></em></div>
<p>For consistency across controls, we recommend the following naming convention.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/naming.jpg"><img height="167" alt="naming" src="http://scorbs.com/wp-content/uploads/2008/06/naming-thumb.jpg" width="433"></a></p>
<p><em><strong></strong></em></p>
<p><em><strong>4.&nbsp; CommonStates and FocusStates are special</strong></em></p>
<p>Many controls define these two state groups:</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/specialstates.jpg"><img height="264" alt="specialstates" src="http://scorbs.com/wp-content/uploads/2008/06/specialstates-thumb.jpg" width="226"></a></p>
<p>If your control is going to have some or all of these states, for consistency, we recommend the above grouping and naming. </p>
<p><strong><em></em></strong></p>
<p><strong><em>5. Be resilient to missing Parts &amp; States in template</em> </strong></p>
<p>There are many reasons why a particular ControlTemplate might not supply a given part or state:&nbsp; the designer may have deliberately chosen to leave it out.&nbsp; He/she might not have created it yet.&nbsp; And so on.</p>
<p>It is good practice to prevent crashes or other catastrophic failures when a part is missing. </p>
<p>Note: The VisualStateManager.GoToState() method already takes care of this for states &#8211; it returns false when the target VisualState is not found.</p>
<p>&nbsp;</p>
<p><strong></strong></p>
<p><em><strong>6.&nbsp; Consider supporting “fallback” states</strong> </em></p>
<p>For complex controls, it is sometimes interesting to provide a fallback mechanism for particular states that do not exist.</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">if</span> (VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, “FocusContent”, useTransitions) == <span style="color: #0000ff">false</span>) </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> {  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>     VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, “Focus", useTransitions);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> } </pre>
</div>
</div>
<p>The advantage of this approach are pretty intuitive:&nbsp; the control continues to visually function correctly when a designer hasn&#8217;t provided a particular state.</p>
<p>But there are also some significant negatives: the fallback states mechanism isn&#8217;t fully integrated into the Parts &amp; States model, which means that Blend has no way of knowing about them. </p>
<p>So, please use fallback states sparingly and only when the control is sufficiently complex enough to warrants it.</p>
<p>Also, if you think this is a pattern that you&#8217;ll leverage often, let us know!&nbsp; We&#8217;d love the feedback.</p>
<p><em><strong></strong></em></p>
<p><em><strong>7.&nbsp; Subclasses should only add states in new state groups (not existing state groups)</strong></em></p>
<p>As you know, each state group is orthogonal.&nbsp; This makes it easy for a subclass to add new state groups.&nbsp; For instance, you can create a StackButton that derives from Button and adds a StackStates group:</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/newstategroup.jpg"><img height="316" alt="newstategroup" src="http://scorbs.com/wp-content/uploads/2008/06/newstategroup-thumb.jpg" width="399"></a></p>
<p>This works because the StackStates state group logic is completely independent from the Button&#8217;s logic around CommonStates &amp; FocusStates.</p>
<p>However, if you want to add a new state to an <u>existing</u> state group, the state group logic can become jumbled.&nbsp; It&#8217;s is difficult to guarantee that the right logical state checks will happen in the right order.</p>
<p>Let&#8217;s make this clearer with an example.&nbsp; BasicControl defines two states in its CommonStates: Normal, MouseOver.&nbsp; Its logical state machine is:</p>
<ul>
<li><em>if (mouse is not over control) goto Normal</em>
<li><em>if (mouse is over control) goto MouseOver</em> </li>
</ul>
<p>Now, ExtendedControl derives from BasicControls and wants to add a Pressed state.&nbsp; The goal logical state machine would be:</p>
<ul>
<li><em>if (mouse is not over control) goto Normal</em>
<li><em>if (mouse is over control <u>AND mouse button is not down</u>) goto MouseOver</em>
<li><em>if (mouse button is down) goto Pressed</em> </li>
</ul>
<p>However, there is no good for way for ExtendedControl to add the (<em>AND mouse button is not down</em>) check for the MouseOver state, since that logic lives in the Button base class.</p>
<p>All this just means:&nbsp; Subclasses can always add new states to an new state group.&nbsp; But we recommend against adding new states to existing state groups.</p>
<p>Note:&nbsp; There are different ways (each with its own pluses &amp; minuses) for the platform to address this limitation in a future version.&nbsp; We&#8217;re currently leaning towards a Triggers-based solution (yes, I said triggers).&nbsp; For more, keep reading.</p>
<p>&nbsp;</p>
<p><strong><u>Looking Ahead</u></strong></p>
<p><strong><em>VSM &amp; Windows Presentation Foundation</em></strong></p>
<p>Silverlight&#8217;s Parts &amp; States Model leverages many features (like ControlTemplates, GetTemplateChild() helpers, etc) that already exist in Windows Presentation Foundation.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/wpf.jpg"><img height="52" alt="wpf" src="http://scorbs.com/wp-content/uploads/2008/06/wpf-thumb.jpg" width="214"></a></p>
<p>However, there are some features &#8211; namely VisualStateManager and its associated classes &#8211; that do not yet exist in WPF.&nbsp;&nbsp; <u>The good news is that the next version of Windows Presentation Foundation will include VisualStateManager!</u></p>
<p>For some that are trying to move their Siverlight 2 controls &amp; skins to WPF now, the next version of the .NET framework may feel a ways off.&nbsp; To help remedy that, we are currently looking into shipping a WPF assembly that contains VisualStateManager before the next full release of WPF.&nbsp; Plans are still early &#8211; and so the timeline &amp; ship vehicle details are still being worked out. </p>
<p>More details as we have them!</p>
<p><strong><em></em></strong></p>
<p><strong><em><u>Future Silverlight Features</u></em></strong></p>
<p>One of the often asked questions about the Silverlight control model is:&nbsp; &#8220;Where are the Triggers?&#8221;&nbsp; In fact, it&#8217;s normally, &#8220;Where are the TRIGGERS?????&#8221;&nbsp; :)</p>
<p>There are a lot of different reasons why we were not able to bring Triggers into the Silverlight 2 release.&nbsp; The primary technical challenge was that our property system architecture is not sufficiently complex to support them.&nbsp; This will however change in a future version of Silverlight, and then we&#8217;ll be able to start supporting Triggers.</p>
<p>How would Triggers &amp; VSM play together?&nbsp; The tentative design brainstorm looks something like:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">PropertyTrigger</span> <span style="color: #ff0000">Property</span><span style="color: #0000ff">="IsPressed"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="True"</span><span style="color: #0000ff">&gt;</span>  </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">GoToState</span> <span style="color: #ff0000">State</span><span style="color: #0000ff">="Pressed"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">PropertyTrigger</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p><strong></strong></p>
<p>The platform would provide a GoToState trigger action that causes VisualStateManager to initiate a state change to the desired state. </p>
<p>The designer would, then, have the option of using the built-in states that come with the control (and leaving the visual state change logic to the control).&nbsp; OR, he/she can take over and trigger all the VSM state changes from XAML.&nbsp; In the latter case, it would also be possible for the designer to add states to new or existing state groups that a control code does not know about.</p>
<p>Exciting stuff coming! </p>
<p>&nbsp;</p>
<p><strong><u>The End</u></strong></p>
<p>So that&#8217;s the end of our 4 part series on the Silverlight 2 Parts &amp; States Model.&nbsp; If you have questions or feedback, we&#8217;d love to hear it.</p>
<p>If this series has wet your appetite for VisualStateManager, here are some more great resources:</p>
<ul>
<li><a href="http://electricbeach.org/">Christian Schormann</a>&#8216;s Blog (Group Program Manager for Expression Blend)
<ul>
<li><a href="http://electricbeach.org/?p=98">Creating Control Skins with Visual State Manager &#8211; An Introduction</a>
<li><a href="http://electricbeach.org/?p=100">Visual State Manager: Goals</a>
<li><a href="http://electricbeach.org/?p=107">Visual State Manager for User Controls: A Simple Chord Finder Example</a> </li>
</ul>
<li>Steve White&#8217;s video tutorials (Program Manager for Expression Blend)
<ul>
<li><a href="http://expression.microsoft.com/en-us/cc643423.aspx ">Expression tutorials</a> </li>
</ul>
<li>Celso Gomes&#8217;s tutorials (Designer for Expression Blend)
<ul>
<li><a title="http://www.nibblestutorials.net/" href="http://www.nibblestutorials.net/">http://www.nibblestutorials.net/</a> </li>
</ul>
<li><a href="http://www.timheuer.com/blog">Tim Heuer</a>&#8216;s Blog (Senior Program Manager on the .NET Developer Platform)
<ul>
<li><a title="http://www.timheuer.com/blog/archive/2008/06/04/skinning-silverlight-controls-made-easier.aspx" href="http://www.timheuer.com/blog/archive/2008/06/04/skinning-silverlight-controls-made-easier.aspx">Skinning Silverlight controls just got easier</a>
<li><a title="http://www.timheuer.com/blog/archive/2008/06/04/silverlight-introduces-visual-state-manager-vsm.aspx" href="http://www.timheuer.com/blog/archive/2008/06/04/silverlight-introduces-visual-state-manager-vsm.aspx">Silverlight and the VisualStateManager</a> </li>
</ul>
<li><a href="http://silverlight.net/blogs/jesseliberty">Jesse Liberty</a>&#8216;s tutorials (Senior Program Manager on the .NET Developer Platform)</li>
<ul>
<li><a href="http://silverlight.net/learn/tutorials/StylesTemplatesVSM.aspx">Styles, Templates, and VisualStateManager</a></li>
</ul>
<li><a href="http://weblogs.asp.net/scottgu">Scott Guthrie</a>&#8216;s Blog (Corporate Vice President of the Developer Division)
<ul>
<li><a title="http://weblogs.asp.net/scottgu/archive/2008/06/06/silverlight-2-beta2-released.aspx" href="http://weblogs.asp.net/scottgu/archive/2008/06/06/silverlight-2-beta2-released.aspx">Silverlight 2 Beta2 Released</a> </li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Parts &amp; States Model with VisualStateManager (Part 3 of 4)</title>
		<link>http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/</link>
		<comments>http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 18:37:22 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/</guid>
		<description><![CDATA[This is the third post in a four part series on Silverlight 2’s Parts &#38; States control model. Last time, you learned how to reskin an existing control using VisualStateManager.  In this post, you’ll see how to build up a &#8230; <a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>This is the third post in a four part series on Silverlight 2’s Parts &amp; States control model.</p>
<p><a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Last time</a>, you learned how to reskin an existing control using VisualStateManager.  In this post, you’ll see how to build up a Parts &amp; States-based custom control.  We’ll also explore how you can create more sophisticated visual transitions.</p>
<p>(Series Link:  <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">Part 1</a>, <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Part 2</a>, <a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Part 3</a>, <a href="http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/">Part 4</a>)</p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">VisualStateManager</span></strong></p>
<p>We’ve saw it briefly in the last post, but let’s formally introduce VisualStateManager.  <img class="wp-smiley" src="http://scorbs.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" /></p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/vsm.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/vsm-thumb.jpg" alt="VSM" width="457" height="181" /></a></p>
<p>VisualStateManager is the class responsible for control visual state management.  The “visual” modifier in that sentence is important &#8211; the control logic remains in charge of the logical state machine.</p>
<p>VSM exposes two main pieces of PME:</p>
<ul>
<li>a <strong>VisualStateGroups attached property</strong>
<ul>
<li>This property is set on the control template’s root visual and contains all the visual states &amp; transitions for that skin</li>
</ul>
</li>
<li>a <strong>static GoToState() method</strong>
<ul>
<li>This method causes VisualStateManager to transition the control’s visuals from one visual state to another.</li>
</ul>
</li>
</ul>
<p>Last time, we concentrated on the VisualStateGroups property in XAML.  Today, we’ll dig into how the control code leverages that GoToState() method.</p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">WeatherControl</span></strong></p>
<p>The custom control that we’ll be looking at today is a simple WeatherControl. The shell of the control code can be found below.  (Note: For readability, I’ve collapsed some of the code snippets.  You can find the <a href="http://scorbs.com/silverlight/weathercontrolpost/complextransitions/WeatherApp.zip">full sample code</a> here.)</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> WeatherControl : Control</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> WeatherControl()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>         DefaultStyleKey = <span style="color: #0000ff">typeof</span>(WeatherControl);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>     <span style="color: #008000">// OnApplyTemplate()</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnApplyTemplate()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>         <span style="color: #0000ff">base</span>.OnApplyTemplate();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>     <span style="color: #008000">// Temperature DP</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty TemperatureProperty = = DependencyProperty.Register(<span style="color: #006080">"Condition"</span>, <span style="color: #0000ff">typeof</span>(Condition), <span style="color: #0000ff">typeof</span>(WeatherControl),<span style="color: #0000ff">null</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Temperature</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>         get { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>         set { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>     <span style="color: #008000">// Condition DP</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty ConditionProperty = DependencyProperty.Register(<span style="color: #006080">"Condition"</span>, <span style="color: #0000ff">typeof</span>(Condition), <span style="color: #0000ff">typeof</span>(WeatherControl), <span style="color: #0000ff">new</span> PropertyMetadata(<span style="color: #0000ff">new</span> PropertyChangedCallback(WeatherControl.OnConditionPropertyChanged)));</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>     <span style="color: #0000ff">public</span> Condition Condition</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>         get { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span>         set { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span>     <span style="color: #008000">// ConditionDescription DP</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> DependencyProperty ConditionDescriptionProperty = DependencyProperty.Register(<span style="color: #006080">"ConditionDescription"</span>, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">string</span>), <span style="color: #0000ff">typeof</span>(WeatherControl), <span style="color: #0000ff">null</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ConditionDescription</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span>         get { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  36:</span>         set { ... }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  37:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  38:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  39:</span>     <span style="color: #008000">// Property change notification</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  40:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> OnConditionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  41:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  42:</span>         WeatherControl weather = d <span style="color: #0000ff">as</span> WeatherControl;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  43:</span>         ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  44:</span>         weather.OnWeatherChange(<span style="color: #0000ff">null</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  45:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  46:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  47:</span>     <span style="color: #008000">// OnWeatherChange virtual</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  48:</span>     <span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> OnWeatherChange(RoutedEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  49:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  50:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  51:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  52:</span> }</pre>
</div>
</div>
<p>You can see that our WeatherControl…</p>
<ul>
<li>is a custom control, deriving from Control.</li>
<li>defines its own built-in style, as indicated by the DefaultStyleKey.</li>
<li>has 3 public dependency properties:
<ul>
<li>Temperature</li>
<li>Condition</li>
<li>ConditionDescription</li>
</ul>
</li>
</ul>
<p>In order to make our WeatherControl skinnable with VSM, we need to:</p>
<ul>
<li>define a control contract</li>
<li>discover &amp; manipulate parts</li>
<li>wire up appropriate state changes using VisualStateManager</li>
</ul>
<p>Here we go!</p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">Defining the Control Contract</span></strong></p>
<p>The control code is responsible for documenting the control contract.  This means it should declare any and all expected Parts and States.  This is done using class level metadata:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> [TemplatePart(Name=<span style="color: #006080">"Core"</span>, Type=<span style="color: #0000ff">typeof</span>(FrameworkElement))]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span> [TemplateVisualState(Name=<span style="color: #006080">"Normal"</span>, GroupName=<span style="color: #006080">"CommonStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span> [TemplateVisualState(Name=<span style="color: #006080">"MouseOver"</span>, GroupName=<span style="color: #006080">"CommonStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span> [TemplateVisualState(Name=<span style="color: #006080">"Pressed"</span>, GroupName=<span style="color: #006080">"CommonStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span> [TemplateVisualState(Name=<span style="color: #006080">"Sunny"</span>, GroupName=<span style="color: #006080">"WeatherStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span> [TemplateVisualState(Name=<span style="color: #006080">"PartlyCloudy"</span>, GroupName=<span style="color: #006080">"WeatherStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span> [TemplateVisualState(Name=<span style="color: #006080">"Cloudy"</span>, GroupName=<span style="color: #006080">"WeatherStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span> [TemplateVisualState(Name=<span style="color: #006080">"Rainy"</span>, GroupName=<span style="color: #006080">"WeatherStates"</span>)]</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> WeatherControl : Control</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>     ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span> }</pre>
</div>
</div>
<p>In the above snippet, there are two attribute classes:</p>
<ul>
<li><strong>TemplatePartAttribute</strong>
<ul>
<li>Specifies the name of the part &amp; expected type</li>
</ul>
</li>
<li><strong>TemplateVisualStateAttribute</strong>
<ul>
<li>Specifies the name of the state &amp; its associated state group</li>
</ul>
</li>
</ul>
<p>This metadata is not used by the runtime.  However, it is leveraged by tools like <a href="http://www.microsoft.com/expression/products/overview.aspx?key=blend">Expression Blend</a> for their skinning support.</p>
<p>These attributes on the WeatherControl give rise to this control bill of materials:</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/contract.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/contract-thumb.jpg" alt="contract" width="425" height="333" /></a></p>
<p>Now, let’s see how the control code manipulates Parts.</p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">Discovering Parts</span></strong></p>
<p>Parts are named elements in the template and need to be manually discovered by the control code.  This is done in the OnApplyTemplate() virtual, which is called whenever a new template is applied.</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">// OnApplyTemplate</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnApplyTemplate()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">base</span>.OnApplyTemplate();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     CorePart = (FrameworkElement)GetTemplateChild(<span style="color: #006080">"Core"</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span> <span style="color: #008000">// private CorePart property</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span> <span style="color: #0000ff">private</span> FrameworkElement CorePart</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>     get</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>         <span style="color: #0000ff">return</span> corePart;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>     set</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>         FrameworkElement oldCorePart = corePart;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>         <span style="color: #0000ff">if</span> (oldCorePart != <span style="color: #0000ff">null</span>)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>             oldCorePart.MouseEnter -= <span style="color: #0000ff">new</span> MouseEventHandler(corePart_MouseEnter);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>             oldCorePart.MouseLeave -= <span style="color: #0000ff">new</span> MouseEventHandler(corePart_MouseLeave);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>             oldCorePart.MouseLeftButtonDown -= <span style="color: #0000ff">new</span> MouseButtonEventHandler(corePart_MouseLeftButtonDown);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>             oldCorePart.MouseLeftButtonUp -= <span style="color: #0000ff">new</span> MouseButtonEventHandler(corePart_MouseLeftButtonUp);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span>         corePart = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span>         <span style="color: #0000ff">if</span> (corePart != <span style="color: #0000ff">null</span>)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>         {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>             corePart.MouseEnter += <span style="color: #0000ff">new</span> MouseEventHandler(corePart_MouseEnter);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span>             corePart.MouseLeave += <span style="color: #0000ff">new</span> MouseEventHandler(corePart_MouseLeave);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span>             corePart.MouseLeftButtonDown += <span style="color: #0000ff">new</span> MouseButtonEventHandler(corePart_MouseLeftButtonDown);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  36:</span>             corePart.MouseLeftButtonUp += <span style="color: #0000ff">new</span> MouseButtonEventHandler(corePart_MouseLeftButtonUp);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  37:</span>         }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  38:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  39:</span> }</pre>
</div>
</div>
<p>To find a named element inside of the template, you use the <strong>GetTemplateChild() </strong>helper method.</p>
<p>In the above example, we discover the “Core” part, which we will use to determine when the control should go into the MouseOver or Pressed states.  Note that the setter logic is resilient to the Core part not being in the template.  This is important, because a control needs to be robust enough to handle a part that is missing or not yet been added.</p>
<p><strong><span style="text-decoration: underline;">Initiating State Changes</span></strong></p>
<p>The control code is responsible for telling VisualStateManager when a visual state change should occur.  Therefore, it must maintain the logical state machine that is associated with the visual state machine.</p>
<p>All of Silverlight 2’s built-in controls create a simple helper method to assist with the state changes.  We recommend that you follow a similar pattern:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">// GoToState() helper</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> GoToState(<span style="color: #0000ff">bool</span> useTransitions)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">//  Go to states in NormalStates state group</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">if</span> (isPressed)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"Pressed"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (isMouseOver)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"MouseOver"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">else</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"Normal"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     <span style="color: #008000">//  Go to states in WeatherStates state group</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">if</span> (Condition ==  Condition.PartlyCloudy)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"PartlyCloudy"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (Condition == Condition.Sunny)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"Sunny"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (Condition == Condition.Cloudy)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"Cloudy"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span>     <span style="color: #0000ff">else</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>         VisualStateManager.GoToState(<span style="color: #0000ff">this</span>, <span style="color: #006080">"Rainy"</span>, useTransitions);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span> }</pre>
</div>
</div>
<p>The GoToState helper method contains a series of if statements which determine the current visual states.  It then tells VisualStateManager to initiate the appropriate state change.  This is done with the static <strong>public static bool</strong> <strong>VisualStateManager.GoToState(Control control, string stateName, bool useTransitions) method.</strong></p>
<p>As you can see, this method…</p>
<ul>
<li>has 3 parameters:
<ul>
<li>control: instance of the control</li>
<li>stateName: name of the visual state to go to</li>
<li>usetTransitions: flag to determine whether transitions should be run in this state change</li>
</ul>
</li>
<li>returns a bool
<ul>
<li>It returns true if the state name was found and false otherwise.</li>
</ul>
</li>
<li>is a no op if…
<ul>
<li>the control was already in the passed in visual state</li>
<li>the visual state cannot be found</li>
</ul>
</li>
</ul>
<p>Most control authors will call their GoToState() helper in 3 places:</p>
<ul>
<li>OnApplyTemplate() with no transitions.
<ul>
<li>When the control first appears, we generally want it to just appear in the appropriate state, and not transition into it.</li>
</ul>
</li>
<li>In certain property change notification handlers</li>
<li>In certain event handlers</li>
</ul>
<p>For our WeatherControl, we add these calls:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">// OnApplyTemplate</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnApplyTemplate()</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">base</span>.OnApplyTemplate();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     CorePart = (FrameworkElement)GetTemplateChild(<span style="color: #006080">"Core"</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     GoToState(<span style="color: #0000ff">false</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span> <span style="color: #008000">// Property Change Notifications</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> OnWeatherChange(RoutedEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>     GoToState(<span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span> <span style="color: #008000">// Event Handlers</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span> <span style="color: #0000ff">void</span> corePart_MouseEnter(<span style="color: #0000ff">object</span> sender, MouseEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>     isMouseOver = <span style="color: #0000ff">true</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>     GoToState(<span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span> <span style="color: #0000ff">void</span> corePart_MouseLeave(<span style="color: #0000ff">object</span> sender, MouseEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>     isMouseOver = <span style="color: #0000ff">false</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>     GoToState(<span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span> <span style="color: #0000ff">void</span> corePart_MouseLeftButtonDown(<span style="color: #0000ff">object</span> sender, MouseButtonEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>     isPressed = <span style="color: #0000ff">true</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>     GoToState(<span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  36:</span> <span style="color: #0000ff">void</span> corePart_MouseLeftButtonUp(<span style="color: #0000ff">object</span> sender, MouseButtonEventArgs e)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  37:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  38:</span>     isPressed = <span style="color: #0000ff">false</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  39:</span>     GoToState(<span style="color: #0000ff">true</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  40:</span> }</pre>
</div>
</div>
<p>For the WeatherControl, we need to initiated state changes when:</p>
<ul>
<li>the template is first applied</li>
<li>the Condition property is changed</li>
<li>mouse events are raised from the Core part</li>
</ul>
<p><strong><span style="text-decoration: underline;">Adding a Built-In Style</span></strong></p>
<p>So now we have our control logic!</p>
<p>I’ve cooked up a very fun (if I do say so myself) ControlTemplate to show off our WeatherControl.  The “fun” makes the template a bit long, however.  Here’s the editted version:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">&lt;!-- VisualStateManager --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">&lt;!-- CommonStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CommonStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>         <span style="color: #008000">&lt;!-- CommonStates States--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Normal"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="MouseOver"</span><span style="color: #0000ff">&gt;<span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt; </span>... <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt;</span></span></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState<span style="color: #0000ff">&gt;</span></span></span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Pressed<span style="color: #0000ff">"</span><span style="color: #0000ff">&gt;<span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt; </span>... <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt;</span></span></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState<span style="color: #0000ff">&gt;</span></span></span></span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>         <span style="color: #008000">&lt;!-- CommonStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.6"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="Pressed"</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.4"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">From</span><span style="color: #0000ff">="Pressed"</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.4"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>     <span style="color: #008000">&lt;!-- WeatherStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="WeatherStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>         <span style="color: #008000">&lt;!-- WeatherStates States--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Sunny"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="PartlyCloudy"<span style="color: #0000ff">"</span><span style="color: #0000ff">&gt;<span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt; </span>... <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt;</span></span></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState<span style="color: #0000ff">&gt;</span></span></span></span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Cloudy<span style="color: #0000ff">"</span><span style="color: #0000ff">&gt;<span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt; </span>... <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt;</span></span></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState<span style="color: #0000ff">&gt;</span></span></span></span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Rainy<span style="color: #0000ff">"</span><span style="color: #0000ff">&gt;<span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt; </span>... <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard<span style="color: #0000ff">&gt;</span></span></span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState<span style="color: #0000ff">&gt;</span></span></span></span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span>         <span style="color: #008000">&lt;!-- WeatherStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.3"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  36:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  37:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>As you can see from this ControlTemplate, I’ve:</p>
<ul>
<li>defined all 7 VisualStates.</li>
<li>leveraged storyboard resources to define the state storyboards</li>
<li>supplied a default VisualTransition for both the CommonStates and WeatherStates</li>
<li>specified VisualTransitionss for certain state changes in the CommonStates</li>
</ul>
<p>Let’s <a href="http://scorbs.com/silverlight/weathercontrolpost/basictransitions/">run it</a>!</p>
<p><a href="http://scorbs.com/silverlight/weathercontrolpost/basictransitions/"><img src="http://scorbs.com/wp-content/uploads/2008/06/basicapp.jpg" alt="basicapp" width="178" height="240" /></a></p>
<p><strong></strong></p>
<p><strong></strong></p>
<p><strong><span style="text-decoration: underline;">Adding Specialized Transitions</span></strong></p>
<p>The generated transitions work well for our WeatherControl. However, being ambitious, let’s add more customized visual transitions between certain state changes.</p>
<p>Here’s our skin in the different weather states:</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/weatherstates.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/weatherstates-thumb.jpg" alt="WeatherStates" width="451" height="310" /></a></p>
<p>When our control goes from Sunny to PartlyCloudy, we don’t want the cloud to to just gradually animate in.  Instead, we’d like it to zoom from the left.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/transitions1.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/transitions-thumb1.jpg" alt="transitions" width="509" height="177" /></a></p>
<p>In order to create a custom transition like this, you can specify an explicit transition storyboard:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">&lt;!-- WeatherStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">&lt;!-- Sunny to PartlyCloudy Transition --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>         <span style="color: #ff0000">From</span><span style="color: #0000ff">="Sunny"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="PartlyCloudy"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>         <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.5"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span> <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0:0:.5"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimationUsingKeyFrames</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>                 <span style="color: #ff0000">BeginTime</span><span style="color: #0000ff">="00:00:00"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="BottomCloud"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">SplineDoubleKeyFrame</span> <span style="color: #ff0000">KeyTime</span><span style="color: #0000ff">="00:00:00"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="-150"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">SplineDoubleKeyFrame</span> <span style="color: #ff0000">KeyTime</span><span style="color: #0000ff">="00:00:00.5000000"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">KeySpline</span><span style="color: #0000ff">="0.173,0.019,1,0.484"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>             <span style="color: #0000ff">&lt;/</span><span style="color: #800000">DoubleAnimationUsingKeyFrames</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimationUsingKeyFrames</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>                 <span style="color: #ff0000">BeginTime</span><span style="color: #0000ff">="00:00:00"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="BottomCloud"</span> <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="(UIElement.Opacity)"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">SplineDoubleKeyFrame</span> <span style="color: #ff0000">KeyTime</span><span style="color: #0000ff">="00:00:00"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="0"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">SplineDoubleKeyFrame</span> <span style="color: #ff0000">KeySpline</span><span style="color: #0000ff">="0.173,0.019,1,1"</span> <span style="color: #ff0000">KeyTime</span><span style="color: #0000ff">="00:00:00.2000000"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="0.1"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">SplineDoubleKeyFrame</span> <span style="color: #ff0000">KeyTime</span><span style="color: #0000ff">="00:00:00.5000000"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="1"</span> <span style="color: #ff0000">KeySpline</span><span style="color: #0000ff">="0,0,1,0.484"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>             <span style="color: #0000ff">&lt;/</span><span style="color: #800000">DoubleAnimationUsingKeyFrames</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualTransition</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>     <span style="color: #008000">&lt;!-- WeatherStates Default Transition --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0:0:.3"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>Now, when VisualStateManager is generating transitions animations for the Sunny to PartlyCloudy state change, it will no longer generate animation for the BottomCloud’s opacity. It will just run the explicit transition storyboard with its two double animations.</p>
<p>To better understand stand how generated transition animations interact with explicit transitions storyboards, let’s look at an example:</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/transitionexample.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/transitionexample-thumb.jpg" alt="transitionexample" width="447" height="291" /></a></p>
<p>Here, we have two visual states: Foo &amp; Bar.  They each animate a different (partially overlapping) set of properties.</p>
<p>How do the the transition animations get built up?</p>
<ul>
<li>VSM will generate transition animations for properties A, C, and D.
<ul>
<li>A, C, and D are animated in one or both of the two states, AND are not animated in the explicit VisualTransition.Storyboard storyboard.</li>
</ul>
</li>
<li>VSM will run the explicit transition storyboard to transition properties B, E, and G
<ul>
<li>B, E, and G are animated by VisualTransitoin.Storyboard.  VSM does not need to generate transition animations for these properties.</li>
</ul>
</li>
<li>VSM will <span style="text-decoration: underline;">not</span> animate the transition of property F.
<ul>
<li>F is animated with an ObjectAnimation in the Foo &amp; Bar states.  It’s not possible for VSM to programmatically generate a linear transition animation for an ObjectAnimation.  Therefore, property F will simply snap into its Bar value after the transition animations have run.</li>
</ul>
</li>
</ul>
<p>Going back to our WeatherControl, I’ve also added explicit transitions for Sunny-&gt;PartlyCloudy, Sunny-&gt;Cloudy, and PartlyCloudy-&gt;Cloudy.</p>
<p><a href="http://scorbs.com/silverlight/weathercontrolpost/complextransitions/">Run the application</a> one last time to see our final look!  You can also grab the <a href="http://scorbs.com/silverlight/weathercontrolpost/complextransitions/WeatherApp.zip">source code</a> here.</p>
<p><a href="http://scorbs.com/silverlight/weathercontrolpost/complextransitions/"><img src="http://scorbs.com/wp-content/uploads/2008/06/basicapp1.jpg" alt="basicapp" width="178" height="240" /></a></p>
<p><strong><span style="text-decoration: underline;">Next time</span></strong></p>
<p>So that’s how to build up a Parts &amp; States-based custom control using VisualStateManager.  I hope you also enjoyed those custom explicit transitions.  <img class="wp-smiley" src="http://scorbs.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" /></p>
<p>Next time, in this series final post, we’ll give some general recommendations on how to use the Parts &amp; States Model.  You’ll also learn about some of our future plans for the Parts &amp; States Model in Silverlight as well as Windows Presentation Foundation!</p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Parts &amp; States Model with VisualStateManager (Part 2 of 4)</title>
		<link>http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/</link>
		<comments>http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 06:15:08 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[skinning]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/</guid>
		<description><![CDATA[This is the second post in a four part series about managing parts &#38; states in Silverlight 2 controls. Today, we’ll put into practice the concepts you learned last time and walk through how to reskin a CheckBox.  (If you &#8230; <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>This is the second post in a four part series about managing parts &amp; states in Silverlight 2 controls.</p>
<p>Today, we’ll put into practice the concepts you learned last time and walk through how to reskin a CheckBox.  (If you haven’t already, please be sure to read <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">part 1</a> of this series.)</p>
<p>Note: I’ve shortened the XAML snippets in this post for readability.  You can download the <a href="http://scorbs.com/silverlight/checkboxskin/final/CheckBoxSkin.zip">full sample code here</a>.</p>
<p>(Series Link:  <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">Part 1</a>, <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Part 2</a>, <a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Part 3</a>, <a href="http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/">Part 4</a>)</p>
<p><strong><span style="text-decoration: underline;">A CheckBox ControlTemplate</span></strong></p>
<p>ControlTemplates define the visuals for a custom control.  The CheckBox ControlTemplate that we’ll be expanding on in this post is below.</p>
<div>Visually:</div>
<div><a href="http://scorbs.com/wp-content/uploads/2008/06/base-checkbox.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/base-checkbox-thumb.jpg" alt="base checkbox" width="181" height="102" /></a></div>
<div>XAML:</div>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">ControlTemplate</span> <span style="color: #ff0000">TargetType</span><span style="color: #0000ff">="CheckBox"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Root"</span> ...<span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>         <span style="color: #008000">&lt;!-- OuterBorder --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Border</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">="20"</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">="20"</span> ... <span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>             <span style="color: #008000">&lt;!-- InnerBorder --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">Border</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="InnerBorder"</span>  ... <span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>                     ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>                     <span style="color: #008000">&lt;!-- Higlight--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Border</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="HighlightBorder"</span> ... <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>                     <span style="color: #008000">&lt;!-- Glow --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Glow"</span> <span style="color: #ff0000">Opacity</span><span style="color: #0000ff">="0"</span> ... <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>                     <span style="color: #008000">&lt;!-- Checkmark Graphic--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Path</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Checkmark"</span> <span style="color: #ff0000">Opacity</span><span style="color: #0000ff">="0"</span> ... <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>                     <span style="color: #008000">&lt;!-- Indeterminate Rect--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="IndeterminateRect"</span> <span style="color: #ff0000">Opacity</span><span style="color: #0000ff">="0"</span> ... <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>                 <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>             <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Border</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Border</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span>         <span style="color: #008000">&lt;!-- ContentPresenter --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">ContentPresenter</span> ...<span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ControlTemplate</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<div>In the above ControlTemplate, you may have noticed that several of the elements have zero opacity.  This is because in the control’s base visual states, those elements  aren’t visible.  However, since there’s no “visual interactivity” yet in this ControlTemplate, when you click on the CheckBox, the visuals don’t change &#8211; those elements remain hidden.</div>
<p>Time to fix that!</p>
<p><strong><span style="text-decoration: underline;">Adding VisualStates &amp; VisualStateGroups</span></strong></p>
<p>As we discussed last time, the Parts &amp; States Model introduces the notion of visual states and visual state groups.</p>
<p>In Silverlight 2, we wanted these ideas to be first class concepts and so we have made them into their own classes: VisualState and VisualStateGroup.  They are managed by the VisualStateManager, which runs the visual state machine for the control.</p>
<p>Let’s see how to add VisualStates &amp; VisualStateGroups to our CheckBox skin!</p>
<p><em><strong>Adding VisualStateGroups to the CheckBox ContrlolTemplate</strong></em></p>
<p>CheckBox has two primary state groups (it has 3 state groups total, but for simplicity, we’re going to ignore the focus group):</p>
<ol>
<li>CommonStates state group</li>
<li>CheckStates state group</li>
</ol>
<p>You add these state groups to the ControlTemplate like this:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">ControlTemplate</span> <span style="color: #ff0000">TargetType</span><span style="color: #0000ff">="CheckBox"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span>        <span style="color: #008000">&lt;!-- Root Visual --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Root"</span> ... <span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>            <span style="color: #008000">&lt;!-- VisualStateManager--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>            <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>                <span style="color: #008000">&lt;!-- CommonStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>                <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CommonStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>                <span style="color: #008000">&lt;!-- CheckStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>                <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CheckStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>        <span style="color: #008000">&lt;!-- Rest of Template --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>        ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ControlTemplate</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span></pre>
</div>
</div>
<p>As you can see from this XAML, to add state groups you need to…</p>
<ul>
<li>use a <strong>vsm xmlns prefix</strong>
<ul>
<li>This is required in Silverlight 2 because of a known bug.</li>
<li>xmlns:vsm=”clr-namespace:System.Windows;assembly=System.Windows”</li>
</ul>
</li>
<li>add a <strong>VisualStateManager.VisualStateManager</strong> property
<ul>
<li>This attached dependency property should be on the ControlTemplate’s root visual.</li>
<li>In it, you define the different VisualStateGroups for that control</li>
</ul>
</li>
<li>include the appropriate <strong>VisualStateGroups</strong>
<ul>
<li>Each VisualStateGroup is named and contains a set of mutually exclusive VisualStates.</li>
</ul>
</li>
</ul>
<p>Now that we’ve added the VisualStateGroups, the next step is to populate them.</p>
<p><em><strong>Adding VisualStates to a VisualStateGroup</strong></em></p>
<p>CheckBox has 7 states spread across these two state groups.  Below is the CheckBox blueprint for its default skin.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/stategroups1.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/stategroups-thumb1.jpg" alt="stategroups" width="424" height="281" /></a></p>
<p>Let’s start by adding the four visual states to our skin’s CommonStates:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CommonStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">&lt;!-- Normal State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Normal"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>     <span style="color: #008000">&lt;!-- MouseOver State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="MouseOver"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="Glow"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="Opacity"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="1"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     <span style="color: #008000">&lt;!-- Pressed State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Pressed"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="HighlightBorder"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="Opacity"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">=".6"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">ColorAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="InnerBorder"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="(Border.BorderBrush).(GradientBrush.GradientStops)[0].(GradientStop.Color)"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="#FF000000"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">ColorAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="InnerBorder"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="(Border.BorderBrush).(GradientBrush.GradientStops)[1].(GradientStop.Color)"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  32:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="#FF000000"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  33:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  35:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  36:</span>     <span style="color: #008000">&lt;!-- Disabled State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  37:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Disabled"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  38:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  39:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  40:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="Root"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  41:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="Opacity"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  42:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">=".7"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  43:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  44:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  45:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  46:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>As seen above, the <strong>VisualState</strong> element…</p>
<ul>
<li>is <strong>named</strong>
<ul>
<li>This is how VisualStateManager looks up a particular state in the template.</li>
</ul>
</li>
<li><strong>contains a Storyboard</strong>
<ul>
<li>This storyboard represents the ControlTemplate’s visual look when it is in that particular state.</li>
<li>It can be a “static” storyboard, meaning that it is a zero duration storyboard whose look is held for the entire time the control is in that state.  All of the above states have “static” storyboards.</li>
<li>It could also be “steady state animation” storyboard.  That is, a non-zero duration storyboard with a Forever repeat behavior.  This represents a storyboard that has a continuously active animation, like a pulsating effect.</li>
</ul>
</li>
</ul>
<p>If you look at the particular states we just defined in our ControlTemplate:</p>
<ul>
<li>The Normal state does not define a Storyboard.  This is because the Normal state looks the same as the “base” look of the ControlTemplate,</li>
<li>The MouseOver state toggles the opacity of the Glow element, giving the illusion of a glow on MouseOver.</li>
<li>The Pressed state changes the color of the checkbox border, as well as updates the opacity of the highlight border.</li>
<li>The Disabled state makes the entire control less opaque.</li>
</ul>
<p>To make this clearer, here’s a screen shot of our skin in these states:</p>
<p><a href="http://scorbs.com/silverlight/checkboxskin/notransitions/" target="_blank"><img src="http://scorbs.com/wp-content/uploads/2008/06/commonstates.jpg" alt="CommonStates" width="519" height="275" /></a></p>
<p>Now, on to the CheckStates:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">&lt;!-- CheckStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CheckStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">&lt;!-- Unchecked State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Unchecked"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>     <span style="color: #008000">&lt;!-- Checked State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Checked"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="Checkmark"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="Opacity"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="1"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>     <span style="color: #008000">&lt;!-- Indeterminate State --&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualState</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="Indeterminate"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">DoubleAnimation</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetName</span><span style="color: #0000ff">="IndeterminateRect"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>                 <span style="color: #ff0000">Storyboard</span>.<span style="color: #ff0000">TargetProperty</span><span style="color: #0000ff">="Opacity"</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>                 <span style="color: #ff0000">Duration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="1"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Storyboard</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualState</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>We populate the CheckStates state group with three VisualStates:</p>
<ul>
<li>The Unchecked state, like the Normal state, does not define a Storyboard.  It will look the same as “base” look of the ControlTemplate,</li>
<li>The Checked state toggles the opacity of the Checkmark graphic.</li>
<li>The Indeterminate state toggles the opacity of the Indeterminate rectangle.</li>
</ul>
<p>Here’s a screen shot of the CheckBox in the different Check states.</p>
<p><a href="http://scorbs.com/silverlight/checkboxskin/notransitions/" target="_blank"><img src="http://scorbs.com/wp-content/uploads/2008/06/checkstates.jpg" alt="CheckStates" width="493" height="224" /></a></p>
<p>Sweet!</p>
<p>So, we’ve created all the VisualStates for our CheckBox. What else do we need to do?</p>
<p>Well, today, the control code is responsible for initiating state changes using VisualStateManager.  (You’ll learn about how the control code does this next time.)  In the ControlTemplate, then, you don’t need to do anything else to have the VisualStates be detected &amp; used.</p>
<p>To see our newly skinned CheckBox in action, run the app <a href="http://scorbs.com/silverlight/checkboxskin/notransitions/">here</a>.</p>
<p><a href="http://scorbs.com/silverlight/checkboxskin/notransitions/" target="_blank"><img src="http://scorbs.com/wp-content/uploads/2008/06/app5.jpg" alt="app" width="240" height="219" /></a></p>
<p><strong></strong></p>
<p><strong>Adding VisualTransitions</strong></p>
<p>When you ran the CheckBox viewer application, you probably noticed that all the VisualStates “snapped” into place.  That makes for a clunky user experience.</p>
<p>What we want is for each visual change to happen gradually.  In Silverlight, you  accomplish this by adding VisualTransitions to the different VisualStateGroups.</p>
<p><em><strong>Adding a Default VisualTransition for a StateGroup</strong></em></p>
<p>Let’s say we want all the states transition in CommonStates to take .5 seconds and all the state transitions in CheckStates to take .2 seconds.  You get this effect by including a default VisualTransition in each state group.</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">&lt;!-- VisualStateManager--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #008000">&lt;!-- CommonStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CommonStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>         <span style="color: #008000">&lt;!-- CommonStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.5"</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>         ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>     <span style="color: #008000">&lt;!-- CheckStates StateGroup--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="CheckStates"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>         <span style="color: #008000">&lt;!-- CheckStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.2"</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span>         ...</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateManager.VisualStateGroups</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>As shown in the above XAML, you create a default <strong>VisualTransition</strong> by adding a VisualTransition to the <strong>VisualStateGroups.Transitions</strong> property.<strong> </strong>VisualTransitions…</p>
<ul>
<li>have a <strong>Duration property</strong>
<ul>
<li>This indicates to VisualStateManager how long you want the transitions to take.</li>
</ul>
</li>
</ul>
<p>VisualStateManager, in turn, creates linear transition animations for all properties animated with…</p>
<ul>
<li>ColorAnimation/ColorAnimationUsingKeyFrames</li>
<li>PointAnimation/PointAnimationUsingKeyFrames</li>
<li>DoubleAnimation/DoubleAnimationusingKeyFrames</li>
</ul>
<p>… in the from and to state storyboards.</p>
<p>What do I mean by this?  Let’s look at an example.</p>
<p>What happens in the transition from Normal to MouseOver?  VisualStateManager detects that the MouseOver state storyboard animates the Glow element’s Opacity property, but the Normal state does not.  It creates a linear DoubleAnimation for the Glow element’s Opacity going from 0 (the value in the Normal state) to 1 (the value in the MouseOver state).</p>
<p>VisualStateManager does this for each state transition: it examines the properties animated in the initial and final state and creates the appropriate transition between the two values.  The result is a control look with gradual transitions created with minimal XAML.</p>
<p>See it for yourself:  <a href="http://scorbs.com/silverlight/checkboxskin/basictransitions/" target="_blank">run our viewer app</a> again.</p>
<p><a href="http://scorbs.com/silverlight/checkboxskin/basictransitions/" target="_blank"><img src="http://scorbs.com/wp-content/uploads/2008/06/app5.jpg" alt="app" width="240" height="219" /></a></p>
<p><em><strong>Creating VisualTransitions for Specific State Changes</strong></em></p>
<p>Adding a default VisualTransition helped the feel of our CheckBox a lot.  But the transitions are still a bit awkward for some of the state changes.</p>
<p>For instance, you may want the visuals to snap when you go from the MouseOver to the Pressed state.  This would help the “click” to feel more for immediate to the user.  You do this by adding a transition and specifying the from and to state:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #008000">&lt;!-- CommonStates Transitions--&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:.5"</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:0.8"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="MouseOver"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0:0:0.2"</span> <span style="color: #ff0000">From</span><span style="color: #0000ff">="Pressed"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">vsm:VisualTransition</span> <span style="color: #ff0000">GeneratedDuration</span><span style="color: #0000ff">="0"</span> <span style="color: #ff0000">From</span><span style="color: #0000ff">="MouseOver"</span> <span style="color: #ff0000">To</span><span style="color: #0000ff">="Pressed"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">vsm:VisualStateGroup.Transitions</span><span style="color: #0000ff">&gt;</span></pre>
</div>
</div>
<p>As you can see, VisualTransitions also have…</p>
<ul>
<li><strong>From &amp; To properties. </strong>
<ul>
<li>These properties hold the name of a particular state</li>
<li>They help VSM pick the right transition for a state change</li>
</ul>
</li>
</ul>
<p>By inspecting these properties, VisualStateManager chooses the most specific transition from the <strong>VisualStateGroup.Transitions</strong> for a particular state change.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/types-of-transitions.jpg"><img src="http://scorbs.com/wp-content/uploads/2008/06/types-of-transitions-thumb.jpg" alt="types of transitions" width="454" height="238" /></a></p>
<p>What happens when our CheckBox goes from MouseOver to Pressed?  VisualStateManager looks first for a From=”MouseOver”/To=”Pressed transition.  If it doesn’t find it, VSM then searches for a To=”Pressed” transition, and then, if necessary, a From=”MouseOver” transition.  Finally, if it still hasn’t found a transition, VSM will use the default transition for that state group.  If there was no default transition defined, it will use a zero length duration.</p>
<p>By giving specific VisualTransition for certain state changes, our CheckBox now looks and feels exactly the way we want!</p>
<p>Give the final app a try <a href="http://scorbs.com/silverlight/checkboxskin/final" target="_blank">here</a>.  You can also grab the <a href="http://scorbs.com/silverlight/checkboxskin/final/checkboxskin.txt">final XAML for the CheckBox skin</a> as well as the <a href="http://scorbs.com/silverlight/checkboxskin/final/CheckBoxSkin.zip">full app sample code</a>.</p>
<p><a href="http://scorbs.com/silverlight/checkboxskin/final" target="_blank"><img src="http://scorbs.com/wp-content/uploads/2008/06/app5.jpg" alt="app" width="240" height="219" /></a></p>
<p><strong><em>Next time</em></strong></p>
<p>Okay, so that’s the basics of how to use VisualStateManager to reskin an existing control.</p>
<p><a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Next time</a>, you’ll see how to build up a custom control from scratch that uses the Parts &amp; States Model.  You’ll also learn about how to use Parts in the template and how to add more complex transitions.</p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Parts &amp; States Model with VisualStateManager (Part 1 of 4)</title>
		<link>http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/</link>
		<comments>http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 01:09:50 +0000</pubDate>
		<dc:creator>Karen</dc:creator>
				<category><![CDATA[silverlight]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[controlmodel]]></category>
		<category><![CDATA[skinning]]></category>
		<category><![CDATA[visualstatemanager]]></category>

		<guid isPermaLink="false">http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/</guid>
		<description><![CDATA[In Silverlight 2, we&#8217;ve added significant new support for managing states and transitions inside of controls.&#160; To help explain the Parts &#38; States Model, I&#8217;ve put together a 4 part post series that will show how to: Create a control &#8230; <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p></p><br /><p>In Silverlight 2, we&#8217;ve added significant new support for managing states and transitions inside of controls.&nbsp; To help explain the Parts &amp; States Model, I&#8217;ve put together a 4 part post series that will show how to:</p>
<ul>
<li>Create a control contract using the Parts &amp; States Model
<li>Wire the control logic to manipulate states &amp; parts
<li>Design a control template with states &amp; parts </li>
</ul>
<p>Playing a starring role in this content will be VisualStateManager, or as we lovingly call it, VSM.&nbsp; VSM can be used with both UserControls and custom controls&#8230; but in this series, we&#8217;ll concentrate on its usage with the latter.</p>
<p>Today&#8217;s post introduces the Parts &amp; States model at a conceptual level.</p>
<p>(Note: these posts assume that you have a basic understanding of UserControls, custom controls &amp; control templates.&nbsp; If you&#8217;re just starting out, check out my <a href="http://scorbs.com/2008/03/09/mix08-creating-rich-dynamic-user-interfaces-with-silverlight-2-controls/">Mix08 controls session</a> or my <a href="http://scorbs.com/2008/06/10/teched-samples/">TechEd controls overview session</a>.)</p>
<p>Let&#8217;s get started!</p>
<p><em> Note: this tutorial has been updated for Silverlight 2 RTW.</em></p>
<p>&nbsp;</p>
<p>(Series Link:&nbsp; <a href="http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/">Part 1</a>, <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/">Part 2</a>, <a href="http://scorbs.com/2008/06/23/parts-states-model-with-visualstatemanager-part-3-of-4/">Part 3</a>, <a href="http://scorbs.com/2008/06/25/parts-states-model-with-visualstatemanager-part-4-of-4/">Part 4</a>)</p>
<p>&nbsp;</p>
<p><strong><u>Motivation for the Parts &amp; States Model</u></strong></p>
<p>Custom controls are a type of Silverlight controls that have a strict separation between the control logic &amp; control visuals.&nbsp; This is great for scenarios where you want to customize the visuals without affecting the logic, and vis versa.</p>
<p>While this strict separation has many benefits, it is often challenging for designers to know what elements in the template the control needs.&nbsp; What is missing is an explicit control contract.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/definingthecontrolcontract2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="255" alt="DefiningTheControlContract2" src="http://scorbs.com/wp-content/uploads/2008/06/definingthecontrolcontract2-thumb.jpg" width="379" border="0"></a></p>
<p>If the control author provides an explicit control contract, the designer has a bill of materials for the control template.&nbsp; This enables easier skinning of controls.</p>
<p>&nbsp;</p>
<p><strong><u>Conceptually:&nbsp; Parts &amp; States Model</u></strong></p>
<p>The Parts &amp; States model is a way of providing that control contract.</p>
<p>It is the recommended way to structure your Silverlight 2 controls.&nbsp; However, this pattern is <u>not</u> enforced by the runtime.&nbsp; You are free to build functioning controls that do not use the Parts &amp; States Model.</p>
<p>That being said, not only do we do think the Parts &amp; States Model is a good model &#8211; it is the model that <a href="http://www.microsoft.com/expression/products/Overview.aspx?key=blend">Expression Blend</a> supports.&nbsp; Therefore, if you want your control to be skinnable in Blend, you should build your control using the Parts &amp; States paradigm.</p>
<p>At the highest level, there are four main concepts in the Parts &amp; States Model:</p>
<ol>
<li>Parts&nbsp;
<li>States
<li>Transitions (between States)
<li>State Groups </li>
</ol>
<p><em><strong>Parts</strong></em></p>
<p>Parts are named elements inside of a control template.&nbsp;&nbsp; The control logic expects these parts to appear in the template because it needs to manipulate them in some way.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/parts.jpg"><img height="128" alt="parts" src="http://scorbs.com/wp-content/uploads/2008/06/parts-thumb.jpg" width="376"></a></p>
<p>In the above slider example, there are 4 parts.&nbsp; Each will be programmatically accessed by the control code.&nbsp; When the UpRepeatButton is pressed, the control code moves the Thumb along the Track to the right.&nbsp; When the DownRepeatButton is pressed, the control code moves the Thumb in the opposite direction.</p>
<p>Not all controls need to programmatically manipulate elements in this way.&nbsp; Such controls (e.g. Button) will not have any Parts in their control contract.</p>
<p><strong>States</strong></p>
<p>Visual states represent the way the control looks in a particular logical state.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/states.jpg"><img height="163" alt="states" src="http://scorbs.com/wp-content/uploads/2008/06/states-thumb.jpg" width="350"></a></p>
<p>For instance, the Button above has a light background when in the MouseOver state, and a dark background when in the Pressed state.</p>
<p><strong>Transitions</strong></p>
<p>Visual transitions represent the way the control looks as it transitions from one visual state to another.</p>
<p><strong><a href="http://scorbs.com/wp-content/uploads/2008/06/transitions.jpg"><img height="163" alt="transitions" src="http://scorbs.com/wp-content/uploads/2008/06/transitions-thumb.jpg" width="429"></a> </strong></p>
<p>Above, Button&#8217;s background gradually fades from a light color to a darker color as it transitions from the MouseOver to the Pressed state.</p>
<p><strong>StateGroups</strong></p>
<p>StateGroup are comprised of mutually exclusive states.&nbsp; State group themselves are orthogonal, meaning that a control can be in 2 different states as long as each of those states are in a different state group.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/stategroups.jpg"><img height="281" alt="stategroups" src="http://scorbs.com/wp-content/uploads/2008/06/stategroups-thumb.jpg" width="424"></a></p>
<p>In the above CheckBox example, there are two state groups: CommonStates and CheckStates.&nbsp; A CheckBox can be in the MouseOver state and the Indeterminate state (for instance) because each of those states are members of a different state groups.&nbsp; On the other hand, it&#8217;s not possible for a CheckBox to be in the Normal and MouseOver state at the same time because they are two states in the same state group.</p>
<p>StateGroups are a new concept that we introduced in Beta2.&nbsp; They help reduce the &#8220;state explosion&#8221; that we saw in the Beta1 model.&nbsp; CheckBox has 7 states in Beta2 (plus 2 focus states).&nbsp; In Beta1, it had (AGH!) 12 states (focus was manipulates as a part and not a state).</p>
<p><strong><em>Initiating State Changes</em></strong></p>
<p>A state change starts when a control detects that, logically, it has changed state.&nbsp; It then initiates a visual state change, causing the appropriate visual transition and then visual state to be shown.</p>
<p><a href="http://scorbs.com/wp-content/uploads/2008/06/logicdiagram.jpg"><img height="252" alt="logicdiagram" src="http://scorbs.com/wp-content/uploads/2008/06/logicdiagram-thumb.jpg" width="425"></a></p>
<p>In the above example, for instance, a control detects a MouseEnter event.&nbsp; It then initiates a visual state change.&nbsp; The control&#8217;s visuals first show the appropriate transition and then rest in the MouseOver visual state.</p>
<p><strong>Next Time</strong></p>
<p>Okay, that&#8217;s the Parts &amp; States Model at the conceptual level.&nbsp; In <a href="http://scorbs.com/2008/06/18/parts-states-model-with-visualstatemanager-part-2-of-4/" target="_blank">Part 2</a> of this series, we&#8217;ll walk through how to skin a CheckBox using the Parts &amp; States Model.</p>
]]></content:encoded>
			<wfw:commentRss>http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

