<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Brandontruong's Weblog</title>
	<atom:link href="http://brandontruong.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://brandontruong.wordpress.com</link>
	<description>Brandon Truong, Web developer, Jquery, Ajax, Igniter, Silverlight</description>
	<lastBuildDate>Thu, 15 Sep 2011 10:50:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='brandontruong.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Brandontruong's Weblog</title>
		<link>http://brandontruong.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://brandontruong.wordpress.com/osd.xml" title="Brandontruong&#039;s Weblog" />
	<atom:link rel='hub' href='http://brandontruong.wordpress.com/?pushpress=hub'/>
		<item>
		<title>What&#8217;s the first thing you think of when you wake up in the morning?</title>
		<link>http://brandontruong.wordpress.com/2011/02/19/whats-the-first-thing-you-think-of-when-you-wake-up-in-the-morning/</link>
		<comments>http://brandontruong.wordpress.com/2011/02/19/whats-the-first-thing-you-think-of-when-you-wake-up-in-the-morning/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 08:33:06 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2011/02/19/whats-the-first-thing-you-think-of-when-you-wake-up-in-the-morning/</guid>
		<description><![CDATA[could I get back to sleep again? lol Ask me anything<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=115&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="formspringmeAnswer">could I get back to sleep again? lol</p>
<p class="formspringmeFooter">
    <a href="http://www.formspring.me/tranphuctruong?utm_medium=social&amp;utm_source=wordpress&amp;utm_campaign=shareanswer">Ask me anything</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=115&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2011/02/19/whats-the-first-thing-you-think-of-when-you-wake-up-in-the-morning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>
	</item>
		<item>
		<title>Winamp Tool kills Silverlight application</title>
		<link>http://brandontruong.wordpress.com/2010/07/28/winamp-tool-kills-silverlight-application/</link>
		<comments>http://brandontruong.wordpress.com/2010/07/28/winamp-tool-kills-silverlight-application/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 10:20:00 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">https://brandontruong.wordpress.com/2010/07/28/winamp-tool-kills-silverlight-application/</guid>
		<description><![CDATA[I have experienced something really strange. When I installed Winamp, a music player, my Silverlight application cant run in Debug mode any more. Anyway, I am just posting about this, so if any one has their Silverlight app can’t run in Debug mode all of the sudden, you might check if you have installed Winamp [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=114&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have experienced something really strange. When I installed <a href="http://www.winamp.com/media-player/en">Winamp</a>, a music player, my Silverlight application cant run in Debug mode any more.</p>
<p>Anyway, I am just posting about this, so if any one has their Silverlight app can’t run in Debug mode all of the sudden, you might check if you have installed Winamp tool or not?</p>
<p>Cheers,</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=114&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/07/28/winamp-tool-kills-silverlight-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>
	</item>
		<item>
		<title>use Enum as an ItemsSource</title>
		<link>http://brandontruong.wordpress.com/2010/04/16/use-enum-as-an-itemssource/</link>
		<comments>http://brandontruong.wordpress.com/2010/04/16/use-enum-as-an-itemssource/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 11:53:18 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/04/16/use-enum-as-an-itemssource/</guid>
		<description><![CDATA[Imagine we have an Enum type and we want to use that Enum type to be an ItemsSource of ListBox or ComboBox. Bolow is an example of an Enum type called AirFareBookingStatus 1: public enum AirFareBookingStatus 2: { 3: AlreadyPaid, 4: Cancelled, 5: Delayed, 6: Deleted, 7: InProcessing, 8: New 9: } What we want [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=113&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Imagine we have an Enum type and we want to use that Enum type to be an ItemsSource of ListBox or ComboBox. Bolow is an example of an Enum type called AirFareBookingStatus</p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.04%;padding-right:4px;direction:ltr;height:167px;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:99.66%;padding-right:0;direction:ltr;height:1px;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> AirFareBookingStatus</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span> {</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum3" style="color:#606060;">   3:</span>     AlreadyPaid,</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:18.96%;padding-right:0;direction:ltr;height:16px;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum4" style="color:#606060;">   4:</span>     Cancelled,</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum5" style="color:#606060;">   5:</span>     Delayed,</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum6" style="color:#606060;">   6:</span>     Deleted,</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum7" style="color:#606060;">   7:</span>     InProcessing,</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum8" style="color:#606060;">   8:</span>     New</pre>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum9" style="color:#606060;">   9:</span> }</pre>
</div>
</div>
<p>What we want to achieve is to populate all the statuses from the Enum to a ListBox or ComboBox.</p>
<p>There is already a solution for this from the <a href="http://forums.silverlight.net/forums/p/88981/205256.aspx" target="_blank">Silverlight forum</a>. The idea is to go through all the Field and put them into a IEnumerable of KeyValuepair<br />
<span style="line-height:16px;"><span style="line-height:normal;"><br />
</span></span></p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.5%;padding-right:4px;direction:ltr;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:104.27%;padding-right:0;direction:ltr;height:94px;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> IEnumerable&lt;KeyValuePair&lt;<span style="color:#0000ff;">string</span>, AirFareBookingStatus&gt;&gt; results;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span> var x = <span style="color:#0000ff;">typeof</span>(AirFareBookingStatus).GetFields().Where(info =&gt; info.FieldType.Equals(<span style="color:#0000ff;">typeof</span>(AirFareBookingStatus)));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum3" style="color:#606060;">   3:</span> results = from field <span style="color:#0000ff;">in</span> x</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum4" style="color:#606060;">   4:</span>           select <span style="color:#0000ff;">new</span> KeyValuePair&lt;<span style="color:#0000ff;">string</span>, AirFareBookingStatus&gt;(field.Name, (AirFareBookingStatus) Enum.Parse(<span style="color:#0000ff;">typeof</span>(AirFareBookingStatus), field.Name, <span style="color:#0000ff;">false</span>));</pre>
</div>
</div>
<p><span style="line-height:16px;"> </span></p>
<p><span style="line-height:normal;">Then all we have to do is assign the result to ListBox or ComboBox ItemsSource</span></p>
<p><span style="line-height:normal;"><br />
</span></p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.5%;padding-right:4px;direction:ltr;height:45px;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:97.44%;padding-right:0;direction:ltr;height:46px;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> lstStatus.ItemsSource = results;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:91.2%;padding-right:0;direction:ltr;height:16px;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span> cmbStatus.ItemsSource = results;</pre>
</div>
</div>
<p><span style="line-height:16px;"><br />
<span style="line-height:normal;">That would be good enough for it to work.</span><br />
</span><br />
But we can even make it more generic by creating a helper function as below</p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.5%;padding-right:4px;direction:ltr;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> Helper</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum3" style="color:#606060;">   3:</span>     <span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> IEnumerable&lt;KeyValuePair&lt;<span style="color:#0000ff;">string</span>, T&gt;&gt; GetEnumList&lt;T&gt;()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum4" style="color:#606060;">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum5" style="color:#606060;">   5:</span>         var x = <span style="color:#0000ff;">typeof</span>(T).GetFields().Where(info =&gt; info.FieldType.Equals(<span style="color:#0000ff;">typeof</span>(T)));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum6" style="color:#606060;">   6:</span>         <span style="color:#0000ff;">return</span> from field <span style="color:#0000ff;">in</span> x</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum7" style="color:#606060;">   7:</span>                select <span style="color:#0000ff;">new</span> KeyValuePair&lt;<span style="color:#0000ff;">string</span>, T&gt;(field.Name, (T) Enum.Parse(<span style="color:#0000ff;">typeof</span>(T), field.Name, <span style="color:#0000ff;">false</span>));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum8" style="color:#606060;">   8:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum9" style="color:#606060;">   9:</span> }</pre>
</div>
</div>
<p>Then, we can call that function for every Enum type we have. For example</p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.77%;padding-right:4px;direction:ltr;height:35px;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:96.38%;padding-right:0;direction:ltr;height:48px;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> lstStatus.ItemsSource = Helper.GetEnumList&lt;AirFareBookingStatus&gt;();</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span> cmbColors.ItemsSource = Helper.GetEnumList&lt;ColorEnum&gt;();</pre>
</div>
</div>
<p>There is still one problem left <span style="font-size:78%;">we</span> have. The item still displays the whole object, so make it display correctly, we need to create DataTemplate resource</p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.61%;padding-right:4px;direction:ltr;height:68px;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:81.13%;padding-right:0;direction:ltr;height:50px;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:94.06%;padding-right:0;direction:ltr;height:16px;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> &lt;DataTemplate x:Key=<span style="color:#006080;">"KeyValuePairDataTemplate"</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span>     &lt;TextBlock Text=<span style="color:#006080;">"{Binding Key}"</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:94.17%;padding-right:0;direction:ltr;height:16px;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum3" style="color:#606060;">   3:</span> &lt;/DataTemplate&gt;</pre>
</div>
</div>
<p>Then apply that DataTemplate to the ItemTemplate of ListBox or ComboBox as below</p>
<div id="codeSnippetWrapper" style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;padding-left:4px;width:97.5%;padding-right:4px;direction:ltr;max-height:200px;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solidfont-size;color:#f4f4f4;">
<div id="codeSnippet" style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;">
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum1" style="color:#606060;">   1:</span> &lt;ListBox x:Name=<span style="color:#006080;">"lstStatus"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum2" style="color:#606060;">   2:</span>     ItemTemplate=<span style="color:#006080;">"{StaticResource KeyValuePairDataTemplate}"</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum3" style="color:#606060;">   3:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:#f4f4f4;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum4" style="color:#606060;">   4:</span> &lt;ComboBox x:Name=<span style="color:#006080;">"cmbColors"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;padding-bottom:0;line-height:12pt;background-color:white;padding-left:0;width:100%;padding-right:0;direction:ltr;overflow:visiblefont-size;color:black;border-style:none;margin:0;"><span id="lnum5" style="color:#606060;">   5:</span>     ItemTemplate=<span style="color:#006080;">"{StaticResource KeyValuePairDataTemplate}"</span> /&gt;</pre>
</div>
</div>
<p>That’s it. Hope that helps.</p>
<p><span style="font-size:130%;">Continue …</span></p>
<p>Now, I have run into another problem. Doing that way above is all good, except the name won’t be as flexible. So, I am going to  <strong><em><span style="text-decoration:underline;">Description</span></em></strong> attribute to display data instead of the normal name of the Enum. To do that, I’ve changed a bit of my Enum like below</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;width:98.52%;direction:ltr;height:266px;max-height:200px;overflow:auto;cursor:text;font-size:8pt;color:#f4f4f4;border:silver 1px solid;padding:4px;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;padding:0;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> AirFareBookingStatus</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum2" style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum3" style="color:#606060;">   3:</span>     [Description(<span style="color:#006080;">"Already Paid"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum4" style="color:#606060;">   4:</span>     AlreadyPaid,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum5" style="color:#606060;">   5:</span>     [Description(<span style="color:#006080;">"i am cancelled"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum6" style="color:#606060;">   6:</span>     Cancelled,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum7" style="color:#606060;">   7:</span>     [Description(<span style="color:#006080;">"I got delayed"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum8" style="color:#606060;">   8:</span>     Delayed,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum9" style="color:#606060;">   9:</span>     [Description(<span style="color:#006080;">"oh man i am about to be deleted"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum10" style="color:#606060;">  10:</span>     Deleted,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum11" style="color:#606060;">  11:</span>     [Description(<span style="color:#006080;">"who is processing me"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum12" style="color:#606060;">  12:</span>     InProcessing,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum13" style="color:#606060;">  13:</span>     [Description(<span style="color:#006080;">"I am brand new babe"</span>)]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum14" style="color:#606060;">  14:</span>     New</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum15" style="color:#606060;">  15:</span> }</pre>
</div>
</div>
<p><span style="line-height:16px;"><br />
<span style="line-height:normal;">Next, I changed a bit in my helper function as well. It’ll get the Description attribute and make it to be Key</span><br />
<span style="line-height:normal;"><br />
</span></span></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;width:99.12%;direction:ltr;height:304px;max-height:200px;overflow:auto;cursor:text;font-size:8pt;color:#f4f4f4;border:silver 1px solid;padding:4px;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;padding:0;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> IEnumerable&lt;KeyValuePair&lt;<span style="color:#0000ff;">string</span>, T&gt;&gt; GetEnumList&lt;T&gt;()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum2" style="color:#606060;">   2:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum3" style="color:#606060;">   3:</span>            var x = <span style="color:#0000ff;">typeof</span>(T).GetFields().Where(info =&gt; info.FieldType.Equals(<span style="color:#0000ff;">typeof</span>(T)));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum4" style="color:#606060;">   4:</span>            <span style="color:#0000ff;">return</span> from field <span style="color:#0000ff;">in</span> x</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum5" style="color:#606060;">   5:</span>                   select <span style="color:#0000ff;">new</span> KeyValuePair&lt;<span style="color:#0000ff;">string</span>, T&gt;(GetEnumDescription(field), (T)Enum.Parse(<span style="color:#0000ff;">typeof</span>(T), field.Name, <span style="color:#0000ff;">false</span>));</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum6" style="color:#606060;">   6:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum7" style="color:#606060;">   7:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum8" style="color:#606060;">   8:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> GetEnumDescription(FieldInfo field)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum9" style="color:#606060;">   9:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum10" style="color:#606060;">  10:</span>            DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(<span style="color:#0000ff;">typeof</span>(DescriptionAttribute), <span style="color:#0000ff;">false</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum11" style="color:#606060;">  11:</span>            <span style="color:#0000ff;">if</span> (attributes.Length &gt; 0)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum12" style="color:#606060;">  12:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum13" style="color:#606060;">  13:</span>                <span style="color:#0000ff;">return</span> attributes[0].Description;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum14" style="color:#606060;">  14:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum15" style="color:#606060;">  15:</span>            <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum16" style="color:#606060;">  16:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum17" style="color:#606060;">  17:</span>                <span style="color:#0000ff;">return</span> field.Name;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum18" style="color:#606060;">  18:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;direction:ltr;overflow:visible;font-size:8pt;color:black;border-style:none;margin:0;padding:0;"><span id="lnum19" style="color:#606060;">  19:</span>        }</pre>
</div>
</div>
<p>Finally, we will have something like this</p>
<p><a href="http://lh5.ggpht.com/_9GiyZFWb_Ys/S8hLKJ7kNeI/AAAAAAAADHM/pkHg_5wHJ4o/s1600-h/Enum%5B2%5D.gif"><img style="display:inline;border:0;" title="Enum" src="http://lh3.ggpht.com/_9GiyZFWb_Ys/S8hLN7ZLhbI/AAAAAAAADHQ/4jCjL62LyyI/Enum_thumb.gif?imgmax=800" border="0" alt="Enum" width="197" height="136" /></a></p>
<p>That’s it. Let me know if you have any comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=113&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/04/16/use-enum-as-an-itemssource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://lh3.ggpht.com/_9GiyZFWb_Ys/S8hLN7ZLhbI/AAAAAAAADHQ/4jCjL62LyyI/Enum_thumb.gif?imgmax=800" medium="image">
			<media:title type="html">Enum</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a Three-Column Silverlight ListBox</title>
		<link>http://brandontruong.wordpress.com/2010/02/03/creating-a-three-column-silverlight-listbox/</link>
		<comments>http://brandontruong.wordpress.com/2010/02/03/creating-a-three-column-silverlight-listbox/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 00:44:56 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/02/03/creating-a-three-column-silverlight-listbox/</guid>
		<description><![CDATA[via expression.microsoft.com<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=110&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry"> <a href='http://posterous.com/getfile/files.posterous.com/brandontruong/fbfmoHecnedhGsjrHevhyhiGFzlFsvDxsevIdokswIgtIIyeciwJjqdIBcDy/media_httpiexpression_tpimd.jpg.scaled1000.jpg'><img src="http://posterous.com/getfile/files.posterous.com/brandontruong/fbfmoHecnedhGsjrHevhyhiGFzlFsvDxsevIdokswIgtIIyeciwJjqdIBcDy/media_httpiexpression_tpimd.jpg.scaled500.jpg" width="500"></a>
<div class="posterous_quote_citation">via <a href="http://expression.microsoft.com/en-us/ee942883.aspx?=mexpnl">expression.microsoft.com</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=110&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/02/03/creating-a-three-column-silverlight-listbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://posterous.com/getfile/files.posterous.com/brandontruong/fbfmoHecnedhGsjrHevhyhiGFzlFsvDxsevIdokswIgtIIyeciwJjqdIBcDy/media_httpiexpression_tpimd.jpg.scaled500.jpg" medium="image" />
	</item>
		<item>
		<title>jQuery source viewer</title>
		<link>http://brandontruong.wordpress.com/2010/01/31/jquery-source-viewer/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/31/jquery-source-viewer/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 23:20:33 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/31/jquery-source-viewer/</guid>
		<description><![CDATA[Check out this website I found at james.padolsey.com This is extremely useful for jQuery developers<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=109&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry">
<div class="posterous_quote_citation">  Check out this website I found at <a href="http://james.padolsey.com/jquery/">james.padolsey.com</a></div>
<p>This is extremely useful for jQuery developers</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=109&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/31/jquery-source-viewer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery UI DatePicker: Disable Specified Days</title>
		<link>http://brandontruong.wordpress.com/2010/01/27/jquery-ui-datepicker-disable-specified-days/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/27/jquery-ui-datepicker-disable-specified-days/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 04:39:39 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/27/jquery-ui-datepicker-disable-specified-days/</guid>
		<description><![CDATA[jQuery UI DatePicker: Disable Specified&#160;Days Written by David Walsh on Tuesday, January 26, 2010 9 &#160;Comments 3 Digg Facebook Reddit StumbleUpon S&#38;S http://davidwalsh.name/jquery-datepicker-disable-days&#8220;&#62;Twitter PDF One project I’m currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI’s DatePicker plugin was the natural choice and it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=108&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry">
<blockquote>
<div>
<h3>jQuery UI DatePicker:  Disable Specified&nbsp;Days</h3>
<p style="color:rgb(153,153,153);"><em>Written by David Walsh on Tuesday, January 26, 2010</em></p>
<div>  		<a href="http://davidwalsh.name/jquery-datepicker-disable-days#comments"><span style="text-indent:0;padding-left:20px;">9 &nbsp;Comments</span></a>  		<a href="http://delicious.com/save?url=http://davidwalsh.name/jquery-datepicker-disable-days&amp;title=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span style="text-indent:0;padding-left:20px;padding-right:5px;text-align:right;">3</span></a>  		<a href="http://digg.com/submit?phase=2&amp;url=http://davidwalsh.name/jquery-datepicker-disable-days&amp;title=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span>Digg</span></a>  		<a href="http://www.facebook.com/sharer.php?u=davidwalsh.name/jquery-datepicker-disable-days&amp;t=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span>Facebook</span></a>  		<a href="http://reddit.com/submit?url=http://davidwalsh.name/jquery-datepicker-disable-days&amp;title=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span>Reddit</span></a>  		<a href="http://stumbleupon.com/submit?url=http://davidwalsh.name/jquery-datepicker-disable-days&amp;title=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span>StumbleUpon</span></a>  		<a href="http://scriptandstyle.com/submit?url=http://davidwalsh.name/jquery-datepicker-disable-days&amp;title=jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days"><span>S&amp;S</span></a>  		<a href="http://twitter.com/home?status=David+Walsh:+jQuery+UI+DatePicker:++Disable+Specified%C2%A0Days:+%3Ca%20href=">http://davidwalsh.name/jquery-datepicker-disable-days</a>&#8220;&gt;<span>Twitter</span>  		<a href="http://davidwalsh.name/wp-content/plugins/as-pdf/generate.php?post=4673"><span>PDF</span></a>  	</div>
<div></div>
<p>  	  		  	<a href="http://davidwalsh.name/dw-content/jquery-datepicker.php"><img src="http://davidwalsh.name/dw-content/jqueryuicalpicker.jpg" height="164" alt="jQuery Calendar Picker" width="196" /></a>
</p>
<p>One project I’m currently working on requires jQuery.  The project also features a datepicker for requesting a visit to their location.  jQuery UI’s DatePicker plugin was the natural choice and it does a really nice job.  One challenge I encountered was the need to prevent specific days from being picked.  Here’s the jQuery javascript I used to accomplish that.</p>
<div><a href="http://davidwalsh.name/dw-content/jquery-datepicker.php">View Demo</a>
<div></div>
</div>
<h3>The jQuery Javascript</h3>
<pre style="display:none;">/* create an array of days which need to be disabled */  var disabledDays = ["2-21-2010","2-24-2010","2-27-2010","2-28-2010","3-3-2010","3-17-2010","4-2-2010","4-3-2010","4-4-2010","4-5-2010"];    /* utility functions */  function nationalDays(date) {  	var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();  	//console.log('Checking (raw): ' + m + '-' + d + '-' + y);  	for (i = 0; i &lt; disabledDays.length; i++) {  		if(ArrayContains(disabledDays,(m+1) + '-' + d + '-' + y) || new Date() &gt; date) {  			//console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);  			return [false];  		}  	}  	//console.log('good:  ' + (m+1) + '-' + d + '-' + y);  	return [true];  }  function noWeekendsOrHolidays(date) {  	var noWeekend = jQuery.datepicker.noWeekends(date);  	return noWeekend[0] ? nationalDays(date) : noWeekend;  }    /* taken from mootools */  function ArrayIndexOf(array,item,from){  	var len = array.length;  	for (var i = (from &lt; 0) ? Math.max(0, len + from) : from || 0; i &lt; len; i++){  		if (array[i] === item) return i;  	}  	return -1;  }  /* taken from mootools */  function ArrayContains(array,item,from){  	return ArrayIndexOf(array,item,from) != -1;  }    /* create datepicker */  jQuery(document).ready(function() {  	jQuery('#date').datepicker({  		minDate: new Date(2010, 0, 1),  		maxDate: new Date(2010, 5, 31),  		dateFormat: 'DD, MM, d, yy',  		constrainInput: true,  		beforeShowDay: noWeekendsOrHolidays  	});  });  </pre>
<div><a href="http://davidwalsh.name/jquery-datepicker-disable-days#" title="Click here to copy this code to the clipboard">Click here to copy this code to the clipboard</a><a href="http://davidwalsh.name/jquery-datepicker-disable-days#" title="Click here to add this snippet to Coda">Click here to add this snippet to Coda</a><a href="http://davidwalsh.name/jquery-datepicker-disable-days#" title="Click here to add this snippet to TextMate">Click here to add this snippet to TextMate</a><a href="http://davidwalsh.name/jquery-datepicker-disable-days#" title="Get the raw code">Get the raw code</a>
<div><span>1</span><span><span>/* create an array of days which need to be disabled */</span><span> </span></span></div>
<div><span>2</span><span><span>var</span><span> disabledDays </span><span>=</span><span> </span><span>[</span><span>"2-21-2010"</span><span>,</span><span>"2-24-2010"</span><span>,</span><span>"2-27-2010"</span><span>,</span><span>"2-28-2010"</span><span>,</span><span>"3-3-2010"</span><span>,</span><span>"3-17-2010"</span><span>,</span><span>"4-2-2010"</span><span>,</span><span>"4-3-2010"</span><span>,</span><span>"4-4-2010"</span><span>,</span><span>"4-5-2010"</span><span>]</span><span>;</span><span> </span></span></div>
<div><span>3</span><span><span> </span></span></div>
<div><span>4</span><span><span>/* utility functions */</span><span> </span></span></div>
<div><span>5</span><span><span>function</span><span> nationalDays</span><span>(</span><span>date</span><span>)</span><span> </span><span>{</span><span> </span></span></div>
<div><span>6</span><span><span>  </span><span>var</span><span> m </span><span>=</span><span> date.</span><span>getMonth</span><span>(</span><span>)</span><span>,</span><span> d </span><span>=</span><span> date.</span><span>getDate</span><span>(</span><span>)</span><span>,</span><span> y </span><span>=</span><span> date.</span><span>getFullYear</span><span>(</span><span>)</span><span>;</span><span> </span></span></div>
<div><span>7</span><span><span> </span><span> //console.log(&#8216;Checking (raw): &#8216; + m + &#8216;-&#8217; + d + &#8216;-&#8217; + y);</span><span> </span></span></div>
<div><span>8</span><span><span>  </span><span>for</span><span> </span><span>(</span><span>i </span><span>=</span><span> </span><span>0</span><span>;</span><span> i </span><span>&lt;</span><span> disabledDays.</span><span>length</span><span>;</span><span> i</span><span>+</span><span>+</span><span>)</span><span> </span><span>{</span><span> </span></span></div>
<div><span>9</span><span><span>    </span><span>if</span><span>(</span><span>ArrayContains</span><span>(</span><span>disabledDays</span><span>,</span><span>(</span><span>m</span><span>+</span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span>&#8216;-&#8217;</span><span> </span><span>+</span><span> d </span><span>+</span><span> </span><span>&#8216;-&#8217;</span><span> </span><span>+</span><span> y</span><span>)</span><span> </span><span>|</span><span>|</span><span> </span><span>new</span><span> Date</span><span>(</span><span>)</span><span> </span><span>&gt;</span><span> date</span><span>)</span><span> </span><span>{</span><span> </span></span></div>
<div><span>10</span><span><span>     </span><span> //console.log(&#8216;bad:  &#8216; + (m+1) + &#8216;-&#8217; + d + &#8216;-&#8217; + y + &#8216; / &#8216; + disabledDays[i]);</span><span> </span></span></div>
<div><span>11</span><span><span>      </span><span>return</span><span> </span><span>[</span><span>false</span><span>]</span><span>;</span><span> </span></span></div>
<div><span>12</span><span><span>    </span><span>}</span><span> </span></span></div>
<div><span>13</span><span><span>  </span><span>}</span><span> </span></span></div>
<div><span>14</span><span><span> </span><span> //console.log(&#8216;good:  &#8216; + (m+1) + &#8216;-&#8217; + d + &#8216;-&#8217; + y);</span><span> </span></span></div>
<div><span>15</span><span><span>  </span><span>return</span><span> </span><span>[</span><span>true</span><span>]</span><span>;</span><span> </span></span></div>
<div><span>16</span><span><span>}</span><span> </span></span></div>
<div><span>17</span><span><span>function</span><span> noWeekendsOrHolidays</span><span>(</span><span>date</span><span>)</span><span> </span><span>{</span><span> </span></span></div>
<div><span>18</span><span><span>  </span><span>var</span><span> noWeekend </span><span>=</span><span> jQuery.</span><span>datepicker</span><span>.</span><span>noWeekends</span><span>(</span><span>date</span><span>)</span><span>;</span><span> </span></span></div>
<div><span>19</span><span><span>  </span><span>return</span><span> noWeekend</span><span>[</span><span>0</span><span>]</span><span> </span><span>?</span><span> nationalDays</span><span>(</span><span>date</span><span>)</span><span> </span><span>:</span><span> noWeekend</span><span>;</span><span> </span></span></div>
<div><span>20</span><span><span>}</span><span> </span></span></div>
<div><span>21</span><span><span> </span></span></div>
<div><span>22</span><span><span>/* taken from mootools */</span><span> </span></span></div>
<div><span>23</span><span><span>function</span><span> ArrayIndexOf</span><span>(</span><span>array</span><span>,</span><span>item</span><span>,</span><span>from</span><span>)</span><span>{</span><span> </span></span></div>
<div><span>24</span><span><span>  </span><span>var</span><span> len </span><span>=</span><span> array.</span><span>length</span><span>;</span><span> </span></span></div>
<div><span>25</span><span><span>  </span><span>for</span><span> </span><span>(</span><span>var</span><span> i </span><span>=</span><span> </span><span>(</span><span>from </span><span>&lt;</span><span> </span><span>0</span><span>)</span><span> </span><span>?</span><span> Math.</span><span>max</span><span>(</span><span>0</span><span>,</span><span> len </span><span>+</span><span> from</span><span>)</span><span> </span><span>:</span><span> from </span><span>|</span><span>|</span><span> </span><span>0</span><span>;</span><span> i </span><span>&lt;</span><span> len</span><span>;</span><span> i</span><span>+</span><span>+</span><span>)</span><span>{</span><span> </span></span></div>
<div><span>26</span><span><span>    </span><span>if</span><span> </span><span>(</span><span>array</span><span>[</span><span>i</span><span>]</span><span> </span><span>=</span><span>=</span><span>=</span><span> </span><span>item</span><span>)</span><span> </span><span>return</span><span> i</span><span>;</span><span> </span></span></div>
<div><span>27</span><span><span>  </span><span>}</span><span> </span></span></div>
<div><span>28</span><span><span>  </span><span>return</span><span> </span><span>-</span><span>1</span><span>;</span><span> </span></span></div>
<div><span>29</span><span><span>}</span><span> </span></span></div>
<div><span>30</span><span><span>/* taken from mootools */</span><span> </span></span></div>
<div><span>31</span><span><span>function</span><span> ArrayContains</span><span>(</span><span>array</span><span>,</span><span>item</span><span>,</span><span>from</span><span>)</span><span>{</span><span> </span></span></div>
<div><span>32</span><span><span>  </span><span>return</span><span> ArrayIndexOf</span><span>(</span><span>array</span><span>,</span><span>item</span><span>,</span><span>from</span><span>)</span><span> </span><span>!</span><span>=</span><span> </span><span>-</span><span>1</span><span>;</span><span> </span></span></div>
<div><span>33</span><span><span>}</span><span> </span></span></div>
<div><span>34</span><span><span> </span></span></div>
<div><span>35</span><span><span>/* create datepicker */</span><span> </span></span></div>
<div><span>36</span><span><span>jQuery</span><span>(</span><span>document</span><span>)</span><span>.</span><span>ready</span><span>(</span><span>function</span><span>(</span><span>)</span><span> </span><span>{</span><span> </span></span></div>
<div><span>37</span><span><span>  jQuery</span><span>(</span><span>&#8216;#date&#8217;</span><span>)</span><span>.</span><span>datepicker</span><span>(</span><span>{</span><span> </span></span></div>
<div><span>38</span><span><span>    minDate</span><span>:</span><span> </span><span>new</span><span> Date</span><span>(</span><span>2010</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>1</span><span>)</span><span>,</span><span> </span></span></div>
<div><span>39</span><span><span>    maxDate</span><span>:</span><span> </span><span>new</span><span> Date</span><span>(</span><span>2010</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>31</span><span>)</span><span>,</span><span> </span></span></div>
<div><span>40</span><span><span>    dateFormat</span><span>:</span><span> </span><span>&#8216;DD, MM, d, yy&#8217;</span><span>,</span><span> </span></span></div>
<div><span>41</span><span><span>    constrainInput</span><span>:</span><span> </span><span>true</span><span>,</span><span> </span></span></div>
<div><span>42</span><span><span>    beforeShowDay</span><span>:</span><span> noWeekendsOrHolidays</span></span></div>
<div><span>43</span><span><span>  </span><span>}</span><span>)</span><span>;</span><span> </span></span></div>
<div><span>44</span><span><span>}</span><span>)</span><span>;</span></span></div>
</div>
<p>The base code is taken from <a href="http://stackoverflow.com/questions/677976/jquery-ui-datepicker-disable-specific-days">this forum post</a>.  You’ll note that I created an array of dates in string format which also accommodates for comparing year.</p>
<div><a href="http://davidwalsh.name/dw-content/jquery-datepicker.php">View Demo</a>
<div></div>
</div>
<p>I’d like to see jQuery UI implement a standard way of disabling days.  Their DatePicker is very nice though so I can’t complain too much!</p>
<div>  <a href="http://davidwalsh.name/wp-content/plugins/as-pdf/generate.php?post=4673">  <span>Download PDF</span>  </a>  </div>
<p> 
<div>
<h3>Related Posts</h3>
<div>
<ul>
<li><a href="http://davidwalsh.name/add-dates-mysql" title="Permanent Link: Adding Days To Dates In&amp;nbsp;MySQL" rel="bookmark">Adding Days To Dates In&nbsp;MySQL</a></li>
<li><a href="http://davidwalsh.name/jquery-create-element" title="Permanent Link: MooTools-Like Element Creation in&amp;nbsp;jQuery" rel="bookmark">MooTools-Like Element Creation in&nbsp;jQuery</a></li>
<li><a href="http://davidwalsh.name/how-to-sniff-firebug-disable" title="Permanent Link: How to Sniff &amp; Disable&amp;nbsp;Firebug" rel="bookmark">How to Sniff &amp; Disable&nbsp;Firebug</a></li>
<li><a href="http://davidwalsh.name/php-function-calculating-days-in-a-month" title="Permanent Link: PHP Function – Calculating Days In A&amp;nbsp;Month" rel="bookmark">PHP Function – Calculating Days In A&nbsp;Month</a></li>
<li><a href="http://davidwalsh.name/firebug-console-log" title="Permanent Link: Logging Information to the Firebug Javascript&amp;nbsp;Console" rel="bookmark">Logging Information to the Firebug Javascript&nbsp;Console</a></li>
</ul>
</div></div>
<p>  		  		  			  &#8211;&gt;
<div style="margin:20px auto 0;">
<div><a href="http://www.elegantthemes.com" title="Premium WordPress Themes" target="_blank"><img src="http://s3.buysellads.com/1242453/11106-1259737184.jpg" height="75" alt="Premium WordPress Themes" width="390" /></a>
</p>
</div></div>
<div></div>
<h3>  		<a href="http://davidwalsh.name/jquery-datepicker-disable-days/feed/" title="Comments::Follow the discussion of this article.">Follow via RSS</a>  				Epic Discussion	</h3>
<p>  	  	<a name="comments"></a>
<div>  				   			<a name="comment-15622"></a>
<div>
<div>  											<img src="http://www.gravatar.com/avatar.php?gravatar_id=47d98a77609dd74e1f0a4570b38fb2f9&amp;size=80&amp;rating=G&amp;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" height="78" alt="Commenter Avatar" width="80" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15622">#</a></span>  				</div>
<div>
<div><span><a href="http://markernst.eu" rel="external nofollow">Mark</a></span> says:</div>
<p>I have to admit, I wish Mootools came with such a wide variaty of UI plugins…</p>
<p>All and all, I think you accomplished your goal very well! Good stuff.</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15623"></a>
<div>
<div>  											<img src="http://www.gravatar.com/avatar.php?gravatar_id=99e081825c99b05900fd4b02faa05737&amp;size=80&amp;rating=G&amp;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" height="80" alt="Commenter Avatar" width="80" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15623">#</a></span>  				</div>
<div>
<div><span>Salih Gedik</span> says:</div>
<p>Nice. But I rarely need jQ. Mootools FTW isn’t it?</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15624"></a>
<div>
<div>  											<img src="http://www.gravatar.com/avatar.php?gravatar_id=154590dd68aea0003457ef8c08054c0e&amp;size=80&amp;rating=G&amp;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" height="80" alt="Commenter Avatar" width="80" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15624">#</a></span>  				</div>
<div>
<div><span><a href="http://scriptsndip.com" rel="external nofollow">Simeon</a></span> says:</div>
<p>Nice post! Can be easily expanded by populating he “disabledDays” using PHP to find all weekends, holidays, etc.  I wonder what the performance impact is though if you span across several years as a typical selection might allow?</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15625"></a>
<div>
<div>  											<img src="http://www.gravatar.com/avatar.php?gravatar_id=b8aa1050fad545fe278326879649a1e5&amp;size=80&amp;rating=G&amp;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" height="80" alt="Commenter Avatar" width="80" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15625">#</a></span>  				</div>
<div>
<div><span><a href="http://www.zcorrecteurs.fr" rel="external nofollow">Savageman</a></span> says:</div>
<p>@mark: you should try this Mootools DatePicker (also works for time): <a href="http://www.monkeyphysics.com/mootools/script/2/datepicker" rel="nofollow">http://www.monkeyphysics.com/mootools/script/2/datepicker</a></p>
<p>Doesn’t have a beforeShowDay option though…</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15626"></a>
<div>
<div>  											<img src="http://www.gravatar.com/avatar.php?gravatar_id=6868c2908859c318f4fa0911eb6029b0&amp;size=80&amp;rating=G&amp;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" height="80" alt="Commenter Avatar" width="80" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15626">#</a></span>  				</div>
<div>
<div><span><a href="http://pixelgraphics.us" rel="external nofollow">Douglas Neiner</a></span> says:</div>
<p>I would remove all the MooTools helpers <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and just use the jQuery native $.inArray function:</p>
<p>Change this line: ArrayContains(disabledDays,(m+1) + ‘-’ + d + ‘-’ + y) to this instead: $.inArray(disabledDays,(m+1) + ‘-’ + d + ‘-’ + y) != -1</p>
<p>Then you don’t need to two other functions from MooTools.</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15627"></a>
<div>
<div>  											<img src="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" height="1" alt="Commenter Avatar" width="1" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15627">#</a></span>  				</div>
<div>
<div><span><a href="http://pixelgraphics.us" rel="external nofollow">Douglas Neiner</a></span> says:</div>
<p>@Douglas Neiner:  Sorry, flip the arguments. $.inArray( value, array )</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15628"></a>
<div>
<div>  											<img src="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" height="1" alt="David Walsh" width="1" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15628">#</a></span>  				</div>
<div>
<div><span><a href="http://davidwalsh.name" rel="external nofollow">David Walsh</a></span> says:</div>
<p>@Douglas Neiner:  I’m a complete MooTools nerd — I didn’t even think to check the jQuery API.  Will update.</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  				   			<a name="comment-15637"></a>
<div>
<div>  											<img src="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" height="1" alt="Commenter Avatar" width="1" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15637">#</a></span>  				</div>
<div>
<div><span><a href="http://www.joshstauffer.com" rel="external nofollow">Josh Stauffer</a></span> says:</div>
<p>Interesting dilema and solution. My problem with the jQuery Datepicker is figuring out how to store selected start and end dates in a cookie.</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
<p>  						   			<a name="comment-15645"></a>
<div>
<div>  											<img src="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" height="1" alt="Commenter Avatar" width="1" />  										<span>January 26 / <a href="http://davidwalsh.name/jquery-datepicker-disable-days#comment-15645">#</a></span>  				</div>
<div>
<div><span>Brad Zickafoose</span> says:</div>
<p>Is it possible to have the DatePicker block out today’s date as well as past dates from being selected, only allowing tomorrow and future dates available for selection?</p>
</p></div>
<div></div>
<p>  			<a href="http://davidwalsh.name/jquery-datepicker-disable-days#">Reply</a></div>
<div></div>
</p></div>
<div></div>
<h3>Be Heard!</h3>
<p>  		I want to hear what you have to say!  Share your comments and questions below.  	</p>
</p>
</div>
</blockquote>
<div class="posterous_quote_citation">via <a href="http://davidwalsh.name/jquery-datepicker-disable-days">davidwalsh.name</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=108&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/27/jquery-ui-datepicker-disable-specified-days/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://davidwalsh.name/dw-content/jqueryuicalpicker.jpg" medium="image">
			<media:title type="html">jQuery Calendar Picker</media:title>
		</media:content>

		<media:content url="http://s3.buysellads.com/1242453/11106-1259737184.jpg" medium="image">
			<media:title type="html">Premium Wordpress Themes</media:title>
		</media:content>

		<media:content url="http://www.gravatar.com/avatar.php?gravatar_id=47d98a77609dd74e1f0a4570b38fb2f9&#38;size=80&#38;rating=G&#38;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://www.gravatar.com/avatar.php?gravatar_id=99e081825c99b05900fd4b02faa05737&#38;size=80&#38;rating=G&#38;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://www.gravatar.com/avatar.php?gravatar_id=154590dd68aea0003457ef8c08054c0e&#38;size=80&#38;rating=G&#38;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://www.gravatar.com/avatar.php?gravatar_id=b8aa1050fad545fe278326879649a1e5&#38;size=80&#38;rating=G&#38;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://www.gravatar.com/avatar.php?gravatar_id=6868c2908859c318f4fa0911eb6029b0&#38;size=80&#38;rating=G&#38;default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fwalshbook3%2Fimages%2Fnoperson.png" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" medium="image">
			<media:title type="html">David Walsh</media:title>
		</media:content>

		<media:content url="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>

		<media:content url="http://davidwalsh.name/jquery-datepicker-disable-days/wp-content/themes/walshbook3/images/blank.gif" medium="image">
			<media:title type="html">Commenter Avatar</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight Unit Testing Framework: Asynchronous Testing of Behaviors</title>
		<link>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-3/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-3/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 03:08:07 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-3/</guid>
		<description><![CDATA[Last month, I bogged about Unit Testing ViewModels AND Views using the Silverlight Unit Testing Framework. I wanted to take that post a step further and talk about some more advanced testing scenarios that are possible. The site itself provides a lot of information about how to get started and what is available with the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=107&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry">
<blockquote>
<div>
<p>Last month, I bogged about <a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html" target="_blank">Unit Testing ViewModels AND Views</a> using the <a href="http://code.msdn.microsoft.com/silverlightut/" title="Silverlight Unit Testing Framework" target="_blank">Silverlight Unit Testing Framework</a>. I wanted to take that post a step further and talk about some more advanced testing scenarios that are possible.  </p>
<p>The site itself provides a lot of information about how to get started and what is available with the framework. One thing to keep in mind that is a radical shift from other testing frameworks is that the Silverlight testing framework runs on the UI thread. This means it does not spawn multiple threads for multiple tests and in fact requires the tests to run &#8220;one at a time&#8221; so they can take advantage of the test surface that is supplied.   </p>
<p>This is a bit different than other frameworks but in my opinion, makes a lot of sense when dealing with Silverlight. The framework provides incredible flexibility for configuring and categorizing your tests.   </p>
<p>If you are searching for a very comprehensive example of the framework in use, look no further than the <a href="http://www.codeplex.com/Silverlight" target="_blank">Silverlight Toolkit</a>. This comes with all source code and in fact uses the testing framework for its tests. You will find not only advanced scenarios for testing, but thousands upon thousands of tests! (I also am guessing that a new standard for PC performance has been invented by mistake &#8230; let&#8217;s all compile the entire toolkit and compare how long it takes!)   </p>
<p><b>Tagging Tests</b></p>
<p>One thing you&#8217;ll find if you run the toolkit tests is that you can enter a tag to filter tests. For example, type in &#8220;Accordion&#8221; to only run the 800 or so unit tests for accordion-type controls.   </p>
<p>To use tag functionality, simply &#8220;tag&#8221; your test like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>[TestClass] </code></span></span></div>
<div><code>2.</code><span><span><code>[Tag(</code><code>"MEF"</code><code>)]</code></span></span></div>
<div><code>3.</code><span><span><code>public</code> <code>class</code> <code>PartModuleTest </code></span></span></div>
<div><code>4.</code><span><span><code>{</code></span></span></div>
<div><code>5.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>I&#8217;ve tagged the test to be a MEF-related test. When I wire up the framework, I can filter the tag like this:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>UnitTestSettings settings = UnitTestSystem.CreateDefaultSettings();</code></span></span></div>
<div><code>2.</code><span><span><code>settings.TagExpression = </code><code>"MEF"</code><code>;</code></span></span></div>
<div><code>3.</code><span><span><code>this</code><code>.RootVisual = UnitTestSystem.CreateTestPage(settings);&nbsp; </code></span></span></div>
</div>
</div>
<p>When I run the tests, only my tests tagged with MEF will run! The toolkit provides an example of a UI that allows you to select the tag, then run the test.  </p>
<p><b>Asynchronous Tests</b></p>
<p>It is often necessary to test methods that are asynchronous or require event coordination. An example may be a service that must wait on return values, or a user control that must be loaded into the framework before you can test it. The Silverlight Unit Testing Framework provides the <code>Asynchronous</code> tag to facilitate this type of test. This tells the framework not to move onto the next test nor consider the current test method complete until an explicit call to <code>TestComplete</code> is made.    </p>
<p>There are several &#8220;helper&#8221; methods supplied for asynchronous processing that we&#8217;ll explore in a minute. To use these methods requires inheriting from one of the base test classes such as <code>SilverlightTest</code> which provides the methods as well as the test surface to add controls to.   </p>
<p>In <a href="http://csharperimage.jeremylikness.com/2009/12/prism-mef-and-mvvm-part-1-of-3-unity.html" title="PRISM, MEF, and MVVM: Unity Glue" target="_blank">PRISM, MEF, and MVVM Part 1 of 3: Unity Glue</a> I explored various options for binding the view model to the view. The 3rd and final method I reviewed was using an attached behavior. I would like to write some unit tests for that behavior (indeed, if I were a test-driven development or TDD purist, I would have written those tests first).   </p>
<p>In order to test the behavior, I need to attach it to a <code>FrameworkElement</code> and then validate it has done what I expected it to do. But how do I go about doing that in our unit test environment?   </p>
<p><b>Attached Behaviors</b></p>
<p>Similar to other controls in other frameworks, Silverlight controls have a distinct life cycle. It varies slightly depending on whether the control has been generated in XAML or through code. There is a great summary table of these events on <a href="http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx" target="_blank">Dave&#8217;s Blog</a>. What&#8217;s important to note is that values and properties are set as soon as you, well, set them, but bindings don&#8217;t take effect until they are inserted into the visual tree. In XAML, the XAML node becomes part of the tree and fires the <code>Loaded</code> event once it is fully integrated. In code, this happens after the element is added as the child of some other element that is in the tree. This allows Silverlight to parse the hierarchy and propagate dependency properties.   </p>
<p>So what we essentially want to do is take our behavior, attach it to an element, and then wait for the <code>Loaded</code> event to fire so we can inspect the element and see that it has been modified accordingly (in this case, we expect that the <code>DataContext</code> property has been set to our view model).   </p>
<p><b>Setting up the Project</b>  </p>
<p>The testing framework provides some handy templates for getting started. I add a new project and select the Silverlight Test Project template. I then add references to the projects I&#8217;ll be testing and the supporting frameworks like PRISM and MEF.  </p>
<p>Next, I&#8217;ll want to build some helper classes to help me test my functionality.  </p>
<p><b>Helper Classes</b>  </p>
<p>I like to create a folder called <code>Helper</code> and place my stubs, mocks, and other helper classes there. These may be utilities, like the <a href="http://csharperimage.jeremylikness.com/2009/08/exception-expected-asserttrue.html" target="_blank">Exception Expected</a> utility I use, or classes that are used for the testing framework.  </p>
<p>First, I&#8217;ll create a test view model with a simple string and string collection property for testing:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>public</code> <code>class</code> <code>TestViewModel </code></span></span></div>
<div><code>02.</code><span><span><code>{</code></span></span></div>
<div><code>03.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>TestViewModel()</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListOfItems = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt;();</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>07.</code><span><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>TestViewModel(List&lt;</code><code>string</code><code>&gt; items)</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListOfItems = items;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>12.</code><span><span>&nbsp;</span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>string</code> <code>Property { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>14.</code><span><span>&nbsp;</span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>List&lt;</code><code>string</code><code>&gt; ListOfItems { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>16.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>If my view models have common methods described in a base class or interface, I might use a mocking framework to mock the class instead.   </p>
<p><b>The Test Class</b>  </p>
<p>The behavior I created has an affinity to the Unity inversion of control (IoC) container. It could be refactored otherwise, but it made sense for the sake of the demonstration. Therefore, I&#8217;ll need to have a container for testing, as well as the view model. My test class starts out looking like this (notice I base it on <code>SilverlightTest</code>):   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestClass]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>class</code> <code>ViewModelBehaviorTest : SilverlightTest</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>const</code> <code>string</code> <code>TESTPROP = </code><code>"Test Property"</code><code>;</code></span></span></div>
<div><code>05.</code><span><span>&nbsp;</span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>IUnityContainer _container;</code></span></span></div>
<div><code>07.</code><span><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestViewModel _viewModel; </code></span></span></div>
<div><code>09.</code><span><span>&nbsp;</span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[ClassInitialize]</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>ClassInitialize()</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_container = </code><code>new</code> <code>UnityContainer();</code></span></span></div>
<div><code>14.</code><span><span>&nbsp;</span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel = </code><code>new</code> <code>TestViewModel() { Property = TESTPROP }; </code></span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_container.RegisterInstance&lt;TestViewModel&gt;(_viewModel); </code></span></span></div>
<div><code>17.</code><span><span>&nbsp;</span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ViewModelBehavior.Container = _container; </code></span></span></div>
<div><code>19.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>20.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>I create a reference to the entire test class for the container and the test view model. When the class is initialized (this is one-time setup, before all tests are run) I create a container, a view model, and tell the container that anytime someone asks for the view model, give them the specific instance I created. I also set the container on the type for the view model behavior class, so it knows what to use when resolving the view model.   </p>
<p><b>The Code Behind Test</b>  </p>
<p>For my first test, I&#8217;ll programmatically attach the behavior and test that it works. The view model behavior takes in a string that is the fully qualified type name for the view model, and then uses the unity container to resolve it. Therefore, my test looks like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>[Asynchronous]</code></span></span></div>
<div><code>03.</code><span><span><code>[Description(</code><code>"Test creating an element and attaching in code behind."</code><code>)]</code></span></span></div>
<div><code>04.</code><span><span><code>public</code> <code>void</code> <code>TestAttach()</code></span></span></div>
<div><code>05.</code><span><span><code>{</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TextBlock textBlock = </code><code>new</code> <code>TextBlock();</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>textBlock.SetValue(ViewModelBehavior.ViewModelProperty, </code><code>typeof</code><code>(TestViewModel).AssemblyQualifiedName);</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>textBlock.Loaded += (o, e) =&gt; </code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(textBlock.DataContext, </code><code>"The data context was never bound."</code><code>);</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreSame(textBlock.DataContext, _viewModel, </code><code>"The data context was not bound to the correct view model."</code><code>);</code></span></span></div>
<div><code>13.</code><span><span>&nbsp;</span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>EnqueueTestComplete();</code></span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>};</code></span></span></div>
<div><code>16.</code><span><span>&nbsp;</span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestPanel.Children.Add(textBlock);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>18.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>There&#8217;s a few things going on here, so let&#8217;s break them down!   </p>
<p>The <code>TestMethod</code> attribute tags this method to be run by the framework. It is decorated with a description, which I can view on the output when the test is run and helps make the test more, ah, descriptive. The first thing I do is create a test block and attach the view model property. Here, I&#8217;m taking the test view model and getting the fully qualified name and using that to set the attached property. We want to make sure everything works fine and there are no errors during binding, so this is where the asynchronous pieces come into play.   </p>
<p>The <code>Asynchronous</code> tag tells the framework that we&#8217;re waiting on events, so don&#8217;t consider this test complete until we explicitly tell the framework it&#8217;s complete. When the text block fires the <code>Loaded</code> event, we confirm that the data context is not null and that it in fact contains the exact instance of the view model we created in the class initialization. Then we tell the framework the test is complete by calling <code>EnqueueTestComplete</code>, which is provided by the base class.   </p>
<p>Finally, if you were to run this without the last line, the test would stall because the text block would never get loaded. We add it as a child of the test surface, and this injects it into the visual tree and fires the loaded event.   </p>
<p><b>The XAML Test</b>  </p>
<p>I&#8217;m not completely confident with this test because the whole reason for creating a behavior was so I could attach the view model in XAML and not use code behind. Therefore, I should really test attaching this behavior through XAML. So, at the top of the test class we&#8217;ll create the necessary XAML and wrap it in a <code>UserControl</code>:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>const</code> <code>string</code> <code>TESTXAML = </code></span></span></div>
<div><code>02.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;UserControl "</code> <code>+</code></span></span></div>
<div><code>03.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>\" "</code>&nbsp; <code>+</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns:x=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>\" "</code> <code>+</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns:vm=\"clr-namespace:PRISMMEF.Common.Behavior;assembly=PRISMMEF.Common\"&gt;"</code> <code>+</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;Grid x:Name=\"LayoutRoot\" Background=\"White\" "</code> <code>+</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"vm:ViewModelBehavior.ViewModel=\"PRISMMEF.Tests.Helper.TestViewModel, PRISMMEF.Tests, Version=1.0.0.0\"&gt;"</code> <code>+</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;ListBox x:Name=\"ListBox\" ItemsSource=\"{Binding ListOfItems}\"/&gt;"</code> <code>+</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;/Grid&gt;&lt;/UserControl&gt;"</code><code>;</code></span></span></div>
</div>
</div>
<p>If you think the constant is ugly, you can always add an actual XAML file, set it as an embedded resource, then read it in instead. That would give you the full functionality of the editor to tweak the test code. Here, we simply create a control with a grid and a list box. The list box uses the attached behavior and also binds the list.  </p>
<p>I want to test the list binding as well, so I add a collection to my test class:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>.</code></span></span></div>
<div><code>2.</code><span><span><code>private</code> <code>static</code> <code>readonly</code> <code>List&lt;</code><code>string</code><code>&gt; _testCollection = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt; { </code><code>"test1"</code><code>, </code><code>"test2"</code> <code>};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>3.</code><span><span><code>.</code></span></span></div>
</div>
</div>
<p>In the class initialize method, I&#8217;ll pass this into the view model&#8217;s constructor so it is set on the <code>ListOfItems</code> property.   </p>
<p>Now, we can create the control from XAML, load it, and test it:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>[Asynchronous]</code></span></span></div>
<div><code>03.</code><span><span><code>[Description(</code><code>"Test creating from XAML"</code><code>)]</code></span></span></div>
<div><code>04.</code><span><span><code>public</code> <code>void</code> <code>TestFromXaml()</code></span></span></div>
<div><code>05.</code><span><span><code>{</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>UserControl control = XamlReader.Load(TESTXAML) </code><code>as</code> <code>UserControl;</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>control.Loaded += (o, e) =&gt; </code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListBox listBox = control.FindName(</code><code>"ListBox"</code><code>) </code><code>as</code> <code>ListBox;</code></span></span></div>
<div><code>11.</code><span><span>&nbsp;</span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(listBox, </code><code>"ListBox was not found."</code><code>);</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(listBox.DataContext, </code><code>"The data context was never bound."</code><code>);</code></span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreSame(listBox.DataContext, _viewModel, </code><code>"The data context was not bound to the correct view model."</code><code>);</code></span></span></div>
<div><code>15.</code><span><span>&nbsp;</span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>IEnumerable&lt;</code><code>string</code><code>&gt; list = listBox.ItemsSource </code><code>as</code> <code>IEnumerable&lt;</code><code>string</code><code>&gt;; </code></span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>List&lt;</code><code>string</code><code>&gt; targetList = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt;(list); </code></span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>CollectionAssert.AreEquivalent(targetList, _testCollection, </code><code>"Collection not properly bound."</code><code>); </code></span></span></div>
<div><code>19.</code><span><span>&nbsp;</span></span></div>
<div><code>20.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>EnqueueTestComplete(); </code></span></span></div>
<div><code>21.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>};</code></span></span></div>
<div><code>22.</code><span><span>&nbsp;</span></span></div>
<div><code>23.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestPanel.Children.Add(control);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>24.</code><span><code>&nbsp;</code><span><code>}</code></span></span></div>
</div>
</div>
<p>Now we load the control from XAML and wire in the <code>Loaded</code> event to test for the data context and the instance. Then, I take the items from the list box itself and compare them with the original list using <code>CollectionAssert</code>. The <code>AreEquivalent</code> does a set comparison. Then we signal the test is complete.  </p>
<p>There&#8217;s no code for this example because it was very straightforward and I&#8217;ll likely be posting a more comprehensive example in the future as the result of a talk I&#8217;ll be giving. Be sure to tune into <a href="http://blogs.msdn.com/geekspeak/" target="_blank">MSDN&#8217;s geekSpeak</a> on Wednesday, February 17th, 2010 when I will be the guest to cover exclusively the topic of the Silverlight Unit Testing Framework (the talks are all stored on the site in case you read this after the event).  </p>
<p>Thanks!  </p>
<p><a href="http://jeremylikness.com/" title="Jeremy Likness"><img title="Jeremy Likness" src="http://jeremylikness.com/signature.gif" border="0" height="39" alt="Jeremy Likness" width="197" /></a></p>
</p>
<div style="clear:both;"></div>
</p></div>
</blockquote>
<div class="posterous_quote_citation">via <a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html">csharperimage.jeremylikness.com</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=107&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://jeremylikness.com/signature.gif" medium="image">
			<media:title type="html">Jeremy Likness</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight Unit Testing Framework: Asynchronous Testing of Behaviors</title>
		<link>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-2/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-2/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 02:33:26 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-2/</guid>
		<description><![CDATA[Last month, I bogged about Unit Testing ViewModels AND Views using the Silverlight Unit Testing Framework. I wanted to take that post a step further and talk about some more advanced testing scenarios that are possible. The site itself provides a lot of information about how to get started and what is available with the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=106&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry">
<blockquote>
<div>
<p>Last month, I bogged about <a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html" target="_blank">Unit Testing ViewModels AND Views</a> using the <a href="http://code.msdn.microsoft.com/silverlightut/" title="Silverlight Unit Testing Framework" target="_blank">Silverlight Unit Testing Framework</a>. I wanted to take that post a step further and talk about some more advanced testing scenarios that are possible.  </p>
<p>The site itself provides a lot of information about how to get started and what is available with the framework. One thing to keep in mind that is a radical shift from other testing frameworks is that the Silverlight testing framework runs on the UI thread. This means it does not spawn multiple threads for multiple tests and in fact requires the tests to run &#8220;one at a time&#8221; so they can take advantage of the test surface that is supplied.   </p>
<p>This is a bit different than other frameworks but in my opinion, makes a lot of sense when dealing with Silverlight. The framework provides incredible flexibility for configuring and categorizing your tests.   </p>
<p>If you are searching for a very comprehensive example of the framework in use, look no further than the <a href="http://www.codeplex.com/Silverlight" target="_blank">Silverlight Toolkit</a>. This comes with all source code and in fact uses the testing framework for its tests. You will find not only advanced scenarios for testing, but thousands upon thousands of tests! (I also am guessing that a new standard for PC performance has been invented by mistake &#8230; let&#8217;s all compile the entire toolkit and compare how long it takes!)   </p>
<p><b>Tagging Tests</b></p>
<p>One thing you&#8217;ll find if you run the toolkit tests is that you can enter a tag to filter tests. For example, type in &#8220;Accordion&#8221; to only run the 800 or so unit tests for accordion-type controls.   </p>
<p>To use tag functionality, simply &#8220;tag&#8221; your test like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>[TestClass] </code></span></span></div>
<div><code>2.</code><span><span><code>[Tag(</code><code>"MEF"</code><code>)]</code></span></span></div>
<div><code>3.</code><span><span><code>public</code> <code>class</code> <code>PartModuleTest </code></span></span></div>
<div><code>4.</code><span><span><code>{</code></span></span></div>
<div><code>5.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>I&#8217;ve tagged the test to be a MEF-related test. When I wire up the framework, I can filter the tag like this:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>UnitTestSettings settings = UnitTestSystem.CreateDefaultSettings();</code></span></span></div>
<div><code>2.</code><span><span><code>settings.TagExpression = </code><code>"MEF"</code><code>;</code></span></span></div>
<div><code>3.</code><span><span><code>this</code><code>.RootVisual = UnitTestSystem.CreateTestPage(settings);&nbsp; </code></span></span></div>
</div>
</div>
<p>When I run the tests, only my tests tagged with MEF will run! The toolkit provides an example of a UI that allows you to select the tag, then run the test.  </p>
<p><b>Asynchronous Tests</b></p>
<p>It is often necessary to test methods that are asynchronous or require event coordination. An example may be a service that must wait on return values, or a user control that must be loaded into the framework before you can test it. The Silverlight Unit Testing Framework provides the <code>Asynchronous</code> tag to facilitate this type of test. This tells the framework not to move onto the next test nor consider the current test method complete until an explicit call to <code>TestComplete</code> is made.    </p>
<p>There are several &#8220;helper&#8221; methods supplied for asynchronous processing that we&#8217;ll explore in a minute. To use these methods requires inheriting from one of the base test classes such as <code>SilverlightTest</code> which provides the methods as well as the test surface to add controls to.   </p>
<p>In <a href="http://csharperimage.jeremylikness.com/2009/12/prism-mef-and-mvvm-part-1-of-3-unity.html" title="PRISM, MEF, and MVVM: Unity Glue" target="_blank">PRISM, MEF, and MVVM Part 1 of 3: Unity Glue</a> I explored various options for binding the view model to the view. The 3rd and final method I reviewed was using an attached behavior. I would like to write some unit tests for that behavior (indeed, if I were a test-driven development or TDD purist, I would have written those tests first).   </p>
<p>In order to test the behavior, I need to attach it to a <code>FrameworkElement</code> and then validate it has done what I expected it to do. But how do I go about doing that in our unit test environment?   </p>
<p><b>Attached Behaviors</b></p>
<p>Similar to other controls in other frameworks, Silverlight controls have a distinct life cycle. It varies slightly depending on whether the control has been generated in XAML or through code. There is a great summary table of these events on <a href="http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx" target="_blank">Dave&#8217;s Blog</a>. What&#8217;s important to note is that values and properties are set as soon as you, well, set them, but bindings don&#8217;t take effect until they are inserted into the visual tree. In XAML, the XAML node becomes part of the tree and fires the <code>Loaded</code> event once it is fully integrated. In code, this happens after the element is added as the child of some other element that is in the tree. This allows Silverlight to parse the hierarchy and propagate dependency properties.   </p>
<p>So what we essentially want to do is take our behavior, attach it to an element, and then wait for the <code>Loaded</code> event to fire so we can inspect the element and see that it has been modified accordingly (in this case, we expect that the <code>DataContext</code> property has been set to our view model).   </p>
<p><b>Setting up the Project</b>  </p>
<p>The testing framework provides some handy templates for getting started. I add a new project and select the Silverlight Test Project template. I then add references to the projects I&#8217;ll be testing and the supporting frameworks like PRISM and MEF.  </p>
<p>Next, I&#8217;ll want to build some helper classes to help me test my functionality.  </p>
<p><b>Helper Classes</b>  </p>
<p>I like to create a folder called <code>Helper</code> and place my stubs, mocks, and other helper classes there. These may be utilities, like the <a href="http://csharperimage.jeremylikness.com/2009/08/exception-expected-asserttrue.html" target="_blank">Exception Expected</a> utility I use, or classes that are used for the testing framework.  </p>
<p>First, I&#8217;ll create a test view model with a simple string and string collection property for testing:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>public</code> <code>class</code> <code>TestViewModel </code></span></span></div>
<div><code>02.</code><span><span><code>{</code></span></span></div>
<div><code>03.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>TestViewModel()</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListOfItems = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt;();</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>07.</code><span><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>TestViewModel(List&lt;</code><code>string</code><code>&gt; items)</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListOfItems = items;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>12.</code><span><span>&nbsp;</span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>string</code> <code>Property { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>14.</code><span><span>&nbsp;</span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>List&lt;</code><code>string</code><code>&gt; ListOfItems { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>16.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>If my view models have common methods described in a base class or interface, I might use a mocking framework to mock the class instead.   </p>
<p><b>The Test Class</b>  </p>
<p>The behavior I created has an affinity to the Unity inversion of control (IoC) container. It could be refactored otherwise, but it made sense for the sake of the demonstration. Therefore, I&#8217;ll need to have a container for testing, as well as the view model. My test class starts out looking like this (notice I base it on <code>SilverlightTest</code>):   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestClass]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>class</code> <code>ViewModelBehaviorTest : SilverlightTest</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>const</code> <code>string</code> <code>TESTPROP = </code><code>"Test Property"</code><code>;</code></span></span></div>
<div><code>05.</code><span><span>&nbsp;</span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>IUnityContainer _container;</code></span></span></div>
<div><code>07.</code><span><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestViewModel _viewModel; </code></span></span></div>
<div><code>09.</code><span><span>&nbsp;</span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[ClassInitialize]</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>ClassInitialize()</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_container = </code><code>new</code> <code>UnityContainer();</code></span></span></div>
<div><code>14.</code><span><span>&nbsp;</span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel = </code><code>new</code> <code>TestViewModel() { Property = TESTPROP }; </code></span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_container.RegisterInstance&lt;TestViewModel&gt;(_viewModel); </code></span></span></div>
<div><code>17.</code><span><span>&nbsp;</span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ViewModelBehavior.Container = _container; </code></span></span></div>
<div><code>19.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>20.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>I create a reference to the entire test class for the container and the test view model. When the class is initialized (this is one-time setup, before all tests are run) I create a container, a view model, and tell the container that anytime someone asks for the view model, give them the specific instance I created. I also set the container on the type for the view model behavior class, so it knows what to use when resolving the view model.   </p>
<p><b>The Code Behind Test</b>  </p>
<p>For my first test, I&#8217;ll programmatically attach the behavior and test that it works. The view model behavior takes in a string that is the fully qualified type name for the view model, and then uses the unity container to resolve it. Therefore, my test looks like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>[Asynchronous]</code></span></span></div>
<div><code>03.</code><span><span><code>[Description(</code><code>"Test creating an element and attaching in code behind."</code><code>)]</code></span></span></div>
<div><code>04.</code><span><span><code>public</code> <code>void</code> <code>TestAttach()</code></span></span></div>
<div><code>05.</code><span><span><code>{</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TextBlock textBlock = </code><code>new</code> <code>TextBlock();</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>textBlock.SetValue(ViewModelBehavior.ViewModelProperty, </code><code>typeof</code><code>(TestViewModel).AssemblyQualifiedName);</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>textBlock.Loaded += (o, e) =&gt; </code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(textBlock.DataContext, </code><code>"The data context was never bound."</code><code>);</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreSame(textBlock.DataContext, _viewModel, </code><code>"The data context was not bound to the correct view model."</code><code>);</code></span></span></div>
<div><code>13.</code><span><span>&nbsp;</span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>EnqueueTestComplete();</code></span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>};</code></span></span></div>
<div><code>16.</code><span><span>&nbsp;</span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestPanel.Children.Add(textBlock);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>18.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>There&#8217;s a few things going on here, so let&#8217;s break them down!   </p>
<p>The <code>TestMethod</code> attribute tags this method to be run by the framework. It is decorated with a description, which I can view on the output when the test is run and helps make the test more, ah, descriptive. The first thing I do is create a test block and attach the view model property. Here, I&#8217;m taking the test view model and getting the fully qualified name and using that to set the attached property. We want to make sure everything works fine and there are no errors during binding, so this is where the asynchronous pieces come into play.   </p>
<p>The <code>Asynchronous</code> tag tells the framework that we&#8217;re waiting on events, so don&#8217;t consider this test complete until we explicitly tell the framework it&#8217;s complete. When the text block fires the <code>Loaded</code> event, we confirm that the data context is not null and that it in fact contains the exact instance of the view model we created in the class initialization. Then we tell the framework the test is complete by calling <code>EnqueueTestComplete</code>, which is provided by the base class.   </p>
<p>Finally, if you were to run this without the last line, the test would stall because the text block would never get loaded. We add it as a child of the test surface, and this injects it into the visual tree and fires the loaded event.   </p>
<p><b>The XAML Test</b>  </p>
<p>I&#8217;m not completely confident with this test because the whole reason for creating a behavior was so I could attach the view model in XAML and not use code behind. Therefore, I should really test attaching this behavior through XAML. So, at the top of the test class we&#8217;ll create the necessary XAML and wrap it in a <code>UserControl</code>:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>const</code> <code>string</code> <code>TESTXAML = </code></span></span></div>
<div><code>02.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;UserControl "</code> <code>+</code></span></span></div>
<div><code>03.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>\" "</code>&nbsp; <code>+</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns:x=\"<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>\" "</code> <code>+</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"xmlns:vm=\"clr-namespace:PRISMMEF.Common.Behavior;assembly=PRISMMEF.Common\"&gt;"</code> <code>+</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;Grid x:Name=\"LayoutRoot\" Background=\"White\" "</code> <code>+</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"vm:ViewModelBehavior.ViewModel=\"PRISMMEF.Tests.Helper.TestViewModel, PRISMMEF.Tests, Version=1.0.0.0\"&gt;"</code> <code>+</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;ListBox x:Name=\"ListBox\" ItemsSource=\"{Binding ListOfItems}\"/&gt;"</code> <code>+</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>"&lt;/Grid&gt;&lt;/UserControl&gt;"</code><code>;</code></span></span></div>
</div>
</div>
<p>If you think the constant is ugly, you can always add an actual XAML file, set it as an embedded resource, then read it in instead. That would give you the full functionality of the editor to tweak the test code. Here, we simply create a control with a grid and a list box. The list box uses the attached behavior and also binds the list.  </p>
<p>I want to test the list binding as well, so I add a collection to my test class:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>.</code></span></span></div>
<div><code>2.</code><span><span><code>private</code> <code>static</code> <code>readonly</code> <code>List&lt;</code><code>string</code><code>&gt; _testCollection = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt; { </code><code>"test1"</code><code>, </code><code>"test2"</code> <code>};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>3.</code><span><span><code>.</code></span></span></div>
</div>
</div>
<p>In the class initialize method, I&#8217;ll pass this into the view model&#8217;s constructor so it is set on the <code>ListOfItems</code> property.   </p>
<p>Now, we can create the control from XAML, load it, and test it:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>[Asynchronous]</code></span></span></div>
<div><code>03.</code><span><span><code>[Description(</code><code>"Test creating from XAML"</code><code>)]</code></span></span></div>
<div><code>04.</code><span><span><code>public</code> <code>void</code> <code>TestFromXaml()</code></span></span></div>
<div><code>05.</code><span><span><code>{</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>UserControl control = XamlReader.Load(TESTXAML) </code><code>as</code> <code>UserControl;</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>control.Loaded += (o, e) =&gt; </code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>ListBox listBox = control.FindName(</code><code>"ListBox"</code><code>) </code><code>as</code> <code>ListBox;</code></span></span></div>
<div><code>11.</code><span><span>&nbsp;</span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(listBox, </code><code>"ListBox was not found."</code><code>);</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(listBox.DataContext, </code><code>"The data context was never bound."</code><code>);</code></span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreSame(listBox.DataContext, _viewModel, </code><code>"The data context was not bound to the correct view model."</code><code>);</code></span></span></div>
<div><code>15.</code><span><span>&nbsp;</span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>IEnumerable&lt;</code><code>string</code><code>&gt; list = listBox.ItemsSource </code><code>as</code> <code>IEnumerable&lt;</code><code>string</code><code>&gt;; </code></span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>List&lt;</code><code>string</code><code>&gt; targetList = </code><code>new</code> <code>List&lt;</code><code>string</code><code>&gt;(list); </code></span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>CollectionAssert.AreEquivalent(targetList, _testCollection, </code><code>"Collection not properly bound."</code><code>); </code></span></span></div>
<div><code>19.</code><span><span>&nbsp;</span></span></div>
<div><code>20.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>EnqueueTestComplete(); </code></span></span></div>
<div><code>21.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>};</code></span></span></div>
<div><code>22.</code><span><span>&nbsp;</span></span></div>
<div><code>23.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestPanel.Children.Add(control);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>24.</code><span><code>&nbsp;</code><span><code>}</code></span></span></div>
</div>
</div>
<p>Now we load the control from XAML and wire in the <code>Loaded</code> event to test for the data context and the instance. Then, I take the items from the list box itself and compare them with the original list using <code>CollectionAssert</code>. The <code>AreEquivalent</code> does a set comparison. Then we signal the test is complete.  </p>
<p>There&#8217;s no code for this example because it was very straightforward and I&#8217;ll likely be posting a more comprehensive example in the future as the result of a talk I&#8217;ll be giving. Be sure to tune into <a href="http://blogs.msdn.com/geekspeak/" target="_blank">MSDN&#8217;s geekSpeak</a> on Wednesday, February 17th, 2010 when I will be the guest to cover exclusively the topic of the Silverlight Unit Testing Framework (the talks are all stored on the site in case you read this after the event).  </p>
<p>Thanks!  </p>
<p><a href="http://jeremylikness.com/" title="Jeremy Likness"><img title="Jeremy Likness" src="http://jeremylikness.com/signature.gif" border="0" height="39" alt="Jeremy Likness" width="197" /></a></p>
</p>
<div style="clear:both;"></div>
</p></div>
</blockquote>
<div class="posterous_quote_citation">via <a href="http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html">csharperimage.jeremylikness.com</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/106/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=106&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/27/silverlight-unit-testing-framework-asynchronous-testing-of-behaviors-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://jeremylikness.com/signature.gif" medium="image">
			<media:title type="html">Jeremy Likness</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit Tests for ViewModels AND Views in Silverlight</title>
		<link>http://brandontruong.wordpress.com/2010/01/27/unit-tests-for-viewmodels-and-views-in-silverlight/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/27/unit-tests-for-viewmodels-and-views-in-silverlight/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 02:07:56 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/27/unit-tests-for-viewmodels-and-views-in-silverlight/</guid>
		<description><![CDATA[Over the past few posts I&#8217;ve been exploring models for modularized Silverlight applications that follow the MVVM pattern (using Prism/CAL). In this post, I&#8217;d like to cover unit testing, and writing appropriate tests not just for the view model, but the view itself. Before we continue, I&#8217;m going to assume you&#8217;ve read: Dynamic Module Loading [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=105&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry">
<blockquote>
<div>
<p>Over the past few posts I&#8217;ve been exploring models for modularized Silverlight applications that follow the MVVM pattern (using <a href="http://compositewpf.codeplex.com/" target="_blank">Prism/CAL</a>). In this post, I&#8217;d like to cover unit testing, and writing appropriate tests not just for the view model, but the view itself.  </p>
<p>Before we continue, I&#8217;m going to assume you&#8217;ve read:  </p>
<ul>
<li><a href="http://csharperimage.jeremylikness.com/2009/11/dynamic-module-loading-with-silverlight.html" target="_blank">Dynamic Module Loading with Silverlight Navigation using Prism</a></li>
<li><a href="http://csharperimage.jeremylikness.com/2009/12/mvvm-composition-in-silverlight-3-with.html" target="_blank">MVVM Composition in Silverlight</a></li>
<li><a href="http://csharperimage.jeremylikness.com/2009/12/simplifying-asynchronous-calls-in.html" target="_blank">Simplifying Asynchronous Calls in Silverlight using Action</a></li>
</ul>
<p>These articles form the core of what I&#8217;m about to discuss. I also want to make sure you&#8217;re familiar with the Silverlight unit testing framework. You can download it and review some articles about how to use it over at the <a href="http://code.msdn.microsoft.com/silverlightut/" target="_blank">Unit Test Framework for Microsoft Silverlight</a> page. I highly recommend pulling down the project and class templates as they will make your life easier!   </p>
<p>The testing framework for Silverlight sets up a project that you run, and that project will then create a visual page that displays the results of tests. What&#8217;s important is that the test framework will not only support class tests, but can also host controls and test the hosted controls as well. Do we even want to do this? I think so.   </p>
<p>Set up your test project and make it the runnable one by adding a new project to your existing Silverlight solution, using the Silverlight project template, then right-clicking on the project and setting it as the start-up project.  </p>
<p>Let&#8217;s get started with a real example. I want to control the visibility of a control based on a boolean value in the view model, so I create a converter that takes in a boolean and returns visibility. I can bind the visibility like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>&lt;</code><code>TextBlock</code> <code>Text</code><code>=</code><code>"Conditional Text"</code> <code>Visibility</code><code>=</code><code>"{Binding ConditionFlag,Converter={StaticResource BoolVisibilityConverter}}"</code><code>&gt;</code></span></span></div>
</div>
</div>
<p>The code for the converter is simple:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>public</code> <code>object</code> <code>Convert(</code><code>object</code> <code>value, Type targetType, </code><code>object</code> <code>parameter, System.Globalization.CultureInfo culture)</code></span></span></div>
<div><code>2.</code><span><span><code>{</code></span></span></div>
<div><code>3.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>return</code> <code>(</code><code>bool</code><code>)value ? Visibility.Visible : Visibility.Collapsed;</code></span></span></div>
<div><code>4.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>To test that we get what we want, simply add a new class in your test project (use the Silverlight Test Class template). With a little bit of typing  you will end up with something like this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestClass]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>class</code> <code>BoolVisibilityConverterTest</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>BoolVisibilityConverter _target;</code></span></span></div>
<div><code>05.</code><span><span>&nbsp;</span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestInitialize]</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>Initialize()</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_target = </code><code>new</code> <code>BoolVisibilityConverter();</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>11.</code><span><span>&nbsp;</span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestMethod]</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>TestTrue()</code></span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>object</code> <code>result = _target.Convert(</code><code>true</code><code>, </code><code>typeof</code><code>(</code><code>bool</code><code>), </code><code>null</code><code>, CultureInfo.CurrentCulture);</code></span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(result, </code><code>"Converter returned null."</code><code>);</code></span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreEqual(Visibility.Visible, result, </code><code>"Converter returned invalid result."</code><code>);</code></span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>19.</code><span><span>&nbsp;</span></span></div>
<div><code>20.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestMethod]</code></span></span></div>
<div><code>21.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>TestFalseNoParameter()</code></span></span></div>
<div><code>22.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>23.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>object</code> <code>result = _target.Convert(</code><code>false</code><code>, </code><code>typeof</code><code>(</code><code>bool</code><code>), </code><code>null</code><code>, CultureInfo.CurrentCulture);</code></span></span></div>
<div><code>24.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(result, </code><code>"Converter returned null."</code><code>);</code></span></span></div>
<div><code>25.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.AreEqual(Visibility.Collapsed, result, </code><code>"Converter returned invalid result."</code><code>);</code></span></span></div>
<div><code>26.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>27.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>Not rocket science there &#8230; but it&#8217;s nice to start out with a few green lights. When you run it, you&#8217;ll see that your two tests passed and all is well (you can, of course, assert something invalid to see what a failure looks like).   </p>
<p>Now let&#8217;s test a view model. Our view model takes in a <code>IService</code> reference so that it can log a user in. It has bindings for username and password and a login command. The service looks like this:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>public</code> <code>interface</code> <code>IService </code></span></span></div>
<div><code>2.</code><span><span><code>{</code></span></span></div>
<div><code>3.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>void</code> <code>Login(</code><code>string</code> <code>username, </code><code>string</code> <code>password, Action&lt;</code><code>bool</code><code>&gt; result); </code></span></span></div>
<div><code>4.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>    So the view model looks like this:
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>001.</code><span><span><code>public</code> <code>class</code> <code>ViewModel : INotifyPropertyChanged </code></span></span></div>
<div><code>002.</code><span><span><code>{</code></span></span></div>
<div><code>003.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>IService _service;</code></span></span></div>
<div><code>004.</code><span><span>&nbsp;</span></span></div>
<div><code>005.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>ViewModel(IService service)</code></span></span></div>
<div><code>006.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>007.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_service = service;</code></span></span></div>
<div><code>008.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>009.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>LoginCommand = </code><code>new</code> <code>DelegateCommand&lt;</code><code>object</code><code>&gt;( o=&gt;CommandLogin );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>010.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}&nbsp; </code></span></span></div>
<div><code>011.</code><span><span>&nbsp;</span></span></div>
<div><code>012.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>bool</code> <code>_isDirty;</code></span></span></div>
<div><code>013.</code><span><span>&nbsp;</span></span></div>
<div><code>014.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>bool</code> <code>IsDirty</code></span></span></div>
<div><code>015.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>016.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>get</code> <code>{ </code><code>return</code> <code>_isDirty; }</code></span></span></div>
<div><code>017.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>018.</code><span><span>&nbsp;</span></span></div>
<div><code>019.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>string</code> <code>_username, _password;</code></span></span></div>
<div><code>020.</code><span><span>&nbsp;</span></span></div>
<div><code>021.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>string</code> <code>Username</code></span></span></div>
<div><code>022.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>023.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>get</code> <code>{ </code><code>return</code> <code>_username; }</code></span></span></div>
<div><code>024.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>set</code></span></span></div>
<div><code>025.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>026.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(value != </code><code>null</code> <code>&amp;&amp; !value.Equals(_username))</code></span></span></div>
<div><code>027.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>028.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_username = value;</code></span></span></div>
<div><code>029.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>OnPropertyChanged(</code><code>"UserName"</code><code>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>030.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>031.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>032.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>033.</code><span><span>&nbsp;</span></span></div>
<div><code>034.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>string</code> <code>Password</code></span></span></div>
<div><code>035.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>036.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>get</code> <code>{ </code><code>return</code> <code>_password; }</code></span></span></div>
<div><code>037.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>set</code></span></span></div>
<div><code>038.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>039.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(value != </code><code>null</code> <code>&amp;&amp; !value.Equals(_password))</code></span></span></div>
<div><code>040.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>041.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_password = value;</code></span></span></div>
<div><code>042.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>OnPropertyChanged(</code><code>"Password"</code><code>);</code></span></span></div>
<div><code>043.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>044.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>045.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>046.</code><span><span>&nbsp;</span></span></div>
<div><code>047.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>DelegateCommand&lt;</code><code>object</code><code>&gt; LoginCommand { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>048.</code><span><span>&nbsp;</span></span></div>
<div><code>049.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>CommandLogin()</code></span></span></div>
<div><code>050.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>051.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(</code><code>string</code><code>.IsNullOrEmpty(_username))</code></span></span></div>
<div><code>052.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>053.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>throw</code> <code>new</code> <code>ValidationException(</code><code>"Username is required."</code><code>);</code></span></span></div>
<div><code>054.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>055.</code><span><span>&nbsp;</span></span></div>
<div><code>056.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(</code><code>string</code><code>.IsNullOrEmpty(_password))</code></span></span></div>
<div><code>057.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>058.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>throw</code> <code>new</code> <code>ValidationException(</code><code>"Password is required."</code><code>);</code></span></span></div>
<div><code>059.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>060.</code><span><span>&nbsp;</span></span></div>
<div><code>061.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_service.Login(_username, _password, (result) =&gt;</code></span></span></div>
<div><code>062.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>063.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(result)</code></span></span></div>
<div><code>064.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>065.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>// logic to navigate to a new page</code></span></span></div>
<div><code>066.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>067.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>else</code></span></span></div>
<div><code>068.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>069.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>throw</code> <code>new</code> <code>ValidationException(</code><code>"The username/password combination is invalid."</code><code>);</code></span></span></div>
<div><code>070.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>071.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>});</code></span></span></div>
<div><code>072.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>} </code></span></span></div>
<div><code>073.</code><span><span>&nbsp;</span></span></div>
<div><code>074.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>protected</code> <code>void</code> <code>OnPropertyChanged(</code><code>string</code> <code>property)</code></span></span></div>
<div><code>075.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></span></span></div>
<div><code>076.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>PropertyChangedEventHandler handler = PropertyChanged;</code></span></span></div>
<div><code>077.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(handler != </code><code>null</code><code>)</code></span></span></div>
<div><code>078.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>079.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>handler(</code><code>this</code><code>, </code><code>new</code> <code>PropertyChangedEventArgs(property));</code></span></span></div>
<div><code>080.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>081.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(!_isDirty)</code></span></span></div>
<div><code>082.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>083.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_isDirty = </code><code>true</code><code>;</code></span></span></div>
<div><code>084.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(handler != </code><code>null</code><code>)</code></span></span></div>
<div><code>085.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>086.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>handler(</code><code>this</code><code>, </code><code>new</code> <code>PropertyChangedEventArgs(</code><code>"IsDirty"</code><code>)); </code></span></span></div>
<div><code>087.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>088.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>089.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>090.</code><span><span>&nbsp;</span></span></div>
<div><code>091.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>ResetDirtyFlag()</code></span></span></div>
<div><code>092.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>093.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(_isDirty)</code></span></span></div>
<div><code>094.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>095.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_isDirty = </code><code>false</code><code>;</code></span></span></div>
<div><code>096.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>PropertyChangedEventHandler handler = PropertyChanged;</code></span></span></div>
<div><code>097.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>if</code> <code>(handler != </code><code>null</code><code>)</code></span></span></div>
<div><code>098.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>099.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>handler(</code><code>this</code><code>, </code><code>new</code> <code>PropertyChangedEventArgs(</code><code>"IsDirty"</code><code>));</code></span></span></div>
<div><code>100.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>101.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>102.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>} </code></span></span></div>
<div><code>103.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>Notice how properties being set should automatically set the &#8220;dirty&#8221; flag as well. I may want to bind my login button to the flag so it only becomes available when the user has changed something, for example. There is also a public method to reset the flag.   </p>
<p>In order to satisfy my service, I&#8217;ll create a &#8220;mock&#8221; object. Why a mock, and not a stub? A stub is a piece of code you put in place to allow something to happen. If I wanted to stub my service, I&#8217;d do this:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>public</code> <code>class</code> <code>ServiceStub : IService</code></span></span></div>
<div><code>2.</code><span><span><code>{</code></span></span></div>
<div><code>3.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>Login(</code><code>string</code> <code>username, </code><code>string</code> <code>password, Action&lt;</code><code>bool</code><code>&gt; result)</code></span></span></div>
<div><code>4.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>5.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>result(</code><code>true</code><code>); </code></span></span></div>
<div><code>6.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>7.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>This would always call back with a valid user and stubs out the functionality so I don&#8217;t have to implement a real login. A mock object, on the other hand, changes. To make this a mock, I do this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>public</code> <code>class</code> <code>ServiceMock : IService </code></span></span></div>
<div><code>02.</code><span><span><code>{</code></span></span></div>
<div><code>03.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>bool</code> <code>LoginCalled { </code><code>get</code><code>; </code><code>set</code><code>; }</code></span></span></div>
<div><code>04.</code><span><span>&nbsp;</span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>Login(</code><code>string</code> <code>username, </code><code>string</code> <code>password, Action&lt;</code><code>bool</code><code>&gt; result)</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>LoginCalled = </code><code>true</code><code>;</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>result(</code><code>true</code><code>); </code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>11.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>The class is a mock because it changes based on how it is used, and then we can query that change to see if our code is doing what we want. So let&#8217;s set up some tests with the view model:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestClass]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>class</code> <code>ViewModelTest</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><span>&nbsp;</span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>ViewModel _target;</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>ServiceMock _service;</code></span></span></div>
<div><code>07.</code><span><span>&nbsp;</span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestInitialize] </code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>Initialize()</code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_service = </code><code>new</code> <code>ServiceMock();</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_target = </code><code>new</code> <code>ViewModel(_service);</code></span></span></div>
<div><code>13.</code><span><span>&nbsp;</span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>15.</code><span><span>&nbsp;</span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestMethod]</code></span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>TestConstructor()</code></span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>19.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsFalse(_target.IsDirty,</code><code>"Dirty flag should not be set."</code><code>); </code></span></span></div>
<div><code>20.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsFalse(_service.LoginCalled,</code><code>"Login should not have been called."</code><code>);</code></span></span></div>
<div><code>21.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsNotNull(_service.LoginCommand, </code><code>"Login command was not set up."</code><code>); </code></span></span></div>
<div><code>22.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>23.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>You can test that the username hasn&#8217;t been populated, for example. Now we can do a little more. In the example, we throw a <code>ValidationException</code> (a custom class) when the username is invalid. The Silverlight 3 validation framework can capture this based on data binding and show appropriate error messages to the client. We want to make sure if we try to login, we throw the exception, so we can do this:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>1.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>2.</code><span><span><code>[ExpectedException(</code><code>typeof</code><code>(ValidationException))]</code></span></span></div>
<div><code>3.</code><span><span><code>public</code> <code>void</code> <code>TestLoginValidation()</code></span></span></div>
<div><code>4.</code><span><span><code>{</code></span></span></div>
<div><code>5.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>_target.CommandLogin(); </code></span></span></div>
<div><code>6.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>Here we call the login command on the empty object and it should throw (and catch) the exception we&#8217;re looking for.  </p>
<p>Finally, to use our mock object, we can set a valid user name and password and call the login command, then verify that the mock object was called:   </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>void</code> <code>TestLogin()</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>_target.Username = </code><code>"Valid Username"</code><code>; </code></span></span></div>
<div><code>05.</code><span><span>&nbsp;</span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>//bonus test: check that the dirty flag got set</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsTrue(_target.IsDirty, </code><code>"Dirty flag was not set on update."</code><code>); </code></span></span></div>
<div><code>08.</code><span><span>&nbsp;</span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>_target.Password = </code><code>"Password"</code><code>; </code></span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>_target.CommandLogin();</code></span></span></div>
<div><code>11.</code><span><span>&nbsp;</span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsTrue(_service.LoginCalled); </code></span></span></div>
<div><code>13.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>After testing your view model, you can then begin to work on testing the view itself. In the &#8220;required reading&#8221; I discussed having a generic view base that would interact with a navigation manager to swap views into and out of view. The views are all contained in an <code>ItemsControl</code>, and register to a view change event. If the view goes out of focus, it moves to a hidden state. If the view comes into focus, it moves to a visible state. While this allows more control over the way the states appear and how to transition between states, there is also the chance someone may add a view and forget wire in the visual state groups. The <code>VisualStateManager</code> won&#8217;t complain, but it can look ugly. We need to test for things like this!   </p>
<p>Fortunately, the testing framework allows for us to host actual views. It provides a testing surface that we add controls to, and those controls are rendered so you can then inspect the visual tree. In this case, we want to emulate a view navigating to a new view and test that it is moved to the correct state.   </p>
<p>Create a new test class. This time, however, we will inherit from the base class <code>SilverlightTest</code> which provides our class with a test panel to host controls on. The set up is a bit more involved, because we need to fold the mock services into the view model, then create the view and glue it all together.  </p>
<p>Before we do this, we&#8217;ll create a helper class called <code>QueryableVisualStateManager</code>. This class is one I borrowed from Justin Angel&#8217;s fantastic blog post about <a href="http://blogs.silverlight.net/blogs/justinangel/archive/2008/12/25/custom-vsm-visualstatemanagers-in-silverlight-2-0.aspx" target="_blank">Custom Visual State Managers</a>. In his post, he details how to create a custom visual state manager that holds a dictionary of the control states so they can be queried later on (in case you&#8217;ve been pulling your hair out in frustration, the framework does not provide direct access to query the current visual state of controls).   </p>
<p>I created the class verbatim, but don&#8217;t care to use it in production code. Instead, we&#8217;ll inject it in our test class. Here&#8217;s the setup:  </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestClass]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>class</code> <code>LoginTest : SilverlightTest</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>Login _login;</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>ViewModel _viewModel;</code></span></span></div>
<div><code>06.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>ServiceMock _service;</code></span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>NavigationManager _navigationManager;</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>private</code> <code>QueryableVisualStateManager _customStateManager; </code></span></span></div>
<div><code>09.</code><span><span>&nbsp;</span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>[TestInitialize]</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>public</code> <code>void</code> <code>TestInitialize()</code></span></span></div>
<div><code>12.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>{</code></span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_login = </code><code>new</code> <code>Login();</code></span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>FrameworkElement root = VisualTreeHelper.GetChild(_login, 0) </code><code>as</code> <code>FrameworkElement;</code></span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>root.SetValue(VisualStateManager.CustomVisualStateManagerProperty, </code><code>new</code> <code>QueryableVisualStateManager()); </code></span></span></div>
<div><code>17.</code><span><span>&nbsp;</span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_service = </code><code>new</code> <code>ServiceMock();</code></span></span></div>
<div><code>19.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_navigationManager = </code><code>new</code> <code>NavigationManager();</code></span></span></div>
<div><code>20.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel = </code><code>new</code> <code>ViewModel(_service);</code></span></span></div>
<div><code>21.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel.Navigation = _navigationManager;</code></span></span></div>
<div><code>22.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_login.DataContext = _viewModel;</code></span></span></div>
<div><code>23.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>TestPanel.Children.Add(_login);</code></span></span></div>
<div><code>24.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>}</code></span></span></div>
<div><code>25.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>What happened? <code>Login</code> is my user control &#8230; it is the view I inject into the shell to show the login page. Here I create an instance of it. Then, I use my friend the <code>VisualTreeHelper</code> to parse the the first child, which is going to be the grid or stack panel or whatever &#8220;host&#8221; control you have inside your user control. Then, I simply set the attached property for the custom view manager to point to the queryable helper class. This will ensure any visual state transitions are recorded in the internal dictionary. Then I wire up my mocks, databind, and finally add the login control to the <code>TestPanel</code>. It now gets hosted on a real test surface and can initialize and display.  </p>
<p>Let&#8217;s assume that the navigation manager I injected is responsible for swapping the view state of the control. The control goes into a <code>HideState</code> when not visible and a <code>ShowState</code> when visible. What I want to test is a simulated login command. We already tested this in the view model, so we can be confident it is going to hit the service and do what it is supposed to do. There is a piece of code that then calls the navigation manager and changes the control&#8217;s state to hidden. We want to test that this hook actually gets fired when the user clicks login, so the login view disappears. Here&#8217;s how:    </p>
<div>
<div>
<div><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#viewSource" title="view source" style="height:16px;">view source</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#printSource" title="print" style="height:16px;">print</a><a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html#about" title="?" style="height:16px;">?</a></div>
</div>
<div>
<div><code>01.</code><span><span><code>[TestMethod]</code></span></span></div>
<div><code>02.</code><span><span><code>public</code> <code>void</code> <code>TestLogin()</code></span></span></div>
<div><code>03.</code><span><span><code>{</code></span></span></div>
<div><code>04.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>const</code> <code>string</code> <code>SHOWSTATE = </code><code>"VisualStates.ShowState"</code><code>;</code></span></span></div>
<div><code>05.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>const</code> <code>string</code> <code>HIDESTATE = </code><code>"VisualStates.HideState"</code><code>;</code></span></span></div>
<div><code>06.</code><span><span>&nbsp;</span></span></div>
<div><code>07.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>// set up this view</code></span></span></div>
<div><code>08.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_navigationManager.NavigateToPage(NavigationManager.NavigationPage.Login);</code></span></span></div>
<div><code>09.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>10.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>string</code> <code>state = QueryableVisualStateManager.QueryState(_login);</code></span></span></div>
<div><code>11.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsTrue(state.Contains(SHOWSTATE) &amp;&amp; !state.Contains(HIDESTATE), </code><code>"Invalid visual state."</code><code>); </code></span></span></div>
<div><code>12.</code><span><span>&nbsp;</span></span></div>
<div><code>13.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>// trigger login </code></span></span></div>
<div><code>14.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel.Username = </code><code>"user"</code><code>;</code></span></span></div>
<div><code>15.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel.Password = </code><code>"password"</code><code>;</code></span></span></div>
<div><code>16.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>_viewModel.CommandLogin();</code></span></span></div>
<div><code>17.</code><span><code>&nbsp;&nbsp;&nbsp;</code><span>&nbsp;</span></span></div>
<div><code>18.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>state = QueryableVisualStateManager.QueryState(_login);</code></span></span></div>
<div><code>19.</code><span><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span><code>Assert.IsTrue(state.Contains(HIDESTATE) &amp;&amp; !state.Contains(SHOWSTATE), </code><code>"Invalid visual state."</code><code>); </code></span></span></div>
<div><code>20.</code><span><span><code>}</code></span></span></div>
</div>
</div>
<p>We first test the pre-condition by navigating to the login page and confirming it has the <code>ShowState</code> and not the <code>HideState</code>. Then, we simulate a login action (this is why command binding and view models are so powerful) and query the state again, testing to make sure we went into a hidden state.  </p>
<p>When you run this test, you might actually see the control flicker for a moment on the screen as it gets initialized on the test surface before it is manipulated and then discarded for other tests. With the right architecture, you are now able to test from the view down to the backend services that drive your application. Now that&#8217;s powerful!  </p>
<p><a href="http://jeremylikness.com/" title="Jeremy Likness"><img title="Jeremy Likness" src="http://jeremylikness.com/signature.gif" border="0" height="39" alt="Jeremy Likness" width="197" /></a></p>
</p>
<div style="clear:both;"></div>
</p></div>
</blockquote>
<div class="posterous_quote_citation">via <a href="http://csharperimage.jeremylikness.com/2009/12/unit-tests-for-viewmodels-and-views-in.html">csharperimage.jeremylikness.com</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=105&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/27/unit-tests-for-viewmodels-and-views-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://jeremylikness.com/signature.gif" medium="image">
			<media:title type="html">Jeremy Likness</media:title>
		</media:content>
	</item>
		<item>
		<title>ondrejsv &#124; How to: Create a new format for sample data in Blend/SketchFlow</title>
		<link>http://brandontruong.wordpress.com/2010/01/27/ondrejsv-how-to-create-a-new-format-for-sample-data-in-blendsketchflow/</link>
		<comments>http://brandontruong.wordpress.com/2010/01/27/ondrejsv-how-to-create-a-new-format-for-sample-data-in-blendsketchflow/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 01:55:33 +0000</pubDate>
		<dc:creator>brandontruong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://brandontruong.wordpress.com/2010/01/27/ondrejsv-how-to-create-a-new-format-for-sample-data-in-blendsketchflow/</guid>
		<description><![CDATA[via blog.ondrejsv.com<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=104&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class='posterous_autopost'>
<div class="posterous_bookmarklet_entry"> <img src="http://posterous.com/getfile/files.posterous.com/brandontruong/wwjeAFqmsuxHtqagmwrbjinHcmnrdEEdohjnDhqpaJqCAdvnakhlsHqjHGJa/media_httpblogondrejs_Jrynn.jpg.scaled500.jpg" width="303">
<div class="posterous_quote_citation">via <a href="http://blog.ondrejsv.com/post/How-to-Create-a-new-format-for-sample-data-in-BlendSketchFlow.aspx">blog.ondrejsv.com</a></div>
</p>
</div>


<!-- No posting client link spam, please. -->


</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brandontruong.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brandontruong.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brandontruong.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brandontruong.wordpress.com&amp;blog=1950271&amp;post=104&amp;subd=brandontruong&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brandontruong.wordpress.com/2010/01/27/ondrejsv-how-to-create-a-new-format-for-sample-data-in-blendsketchflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/078e01527cc65cb1786bcec439ef4545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">brandontruong</media:title>
		</media:content>

		<media:content url="http://posterous.com/getfile/files.posterous.com/brandontruong/wwjeAFqmsuxHtqagmwrbjinHcmnrdEEdohjnDhqpaJqCAdvnakhlsHqjHGJa/media_httpblogondrejs_Jrynn.jpg.scaled500.jpg" medium="image" />
	</item>
	</channel>
</rss>
