<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Dev102.com</title>
	
	<link>http://www.dev102.com</link>
	<description>What I Know, You Know...</description>
	<pubDate>Wed, 07 Mar 2012 13:38:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Dev102feed" /><feedburner:info uri="dev102feed" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><feedburner:emailServiceId>Dev102feed</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>2 Important Things to Notice When Using Lock() Statement</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/O9QxcjlglNg/</link>
		<comments>http://www.dev102.com/2012/02/26/2-important-things-to-notice-when-using-lock-statement/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 18:22:35 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[lock]]></category>

		<category><![CDATA[multithreading]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1321</guid>
		<description><![CDATA[Hi

Every one knows that when different threads write to the same data structure they need to be synced.

Lets assume we have a Service that exposed a single function Add which added the value received to a List.
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ListWrapper</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> List&#60;<span style="color: #0000ff">int</span>&#62; m_list =<span style="color: #0000ff">new</span> List&#60;<span style="color: #0000ff">int</span>&#62;();</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060">   4:</span></pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Add(<span style="color: #0000ff">int</span> i)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060">   6:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060">   7:</span>         m_list.Add(i);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060">   8:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060">   9:</span> }</pre>
<!--CRLF--></div>
</div>
In order to make it thread safe we would simply lock the List so if multiple thread try to access this function they will add their values to the list each at a time.
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Add(<span style="color: #0000ff">int</span> i)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">lock</span> (m_list)</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060">   5:</span>         m_list.Add(i);</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<!--CRLF-->
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &#34;Courier New&#34;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060">   7:</span> }</pre>
<!--CRLF--></div>
</div>
For 99.9% of the applications this is a good solution.
]]></description>
			<content:encoded><![CDATA[<p>Hi</p>
<p>Every one knows that when different threads write to the same data structure they need to be synced.</p>
<p>Lets assume we have a Service that exposed a single function Add which added the value received to a List.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ListWrapper</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> List&lt;<span style="color: #0000ff">int</span>&gt; m_list =<span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060">   4:</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Add(<span style="color: #0000ff">int</span> i)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060">   6:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060">   7:</span>         m_list.Add(i);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060">   8:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060">   9:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>In order to make it thread safe we would simply lock the List so if multiple thread try to access this function they will add their values to the list each at a time.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Add(<span style="color: #0000ff">int</span> i)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">lock</span> (m_list)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060">   5:</span>         m_list.Add(i);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060">   7:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>For 99.9% of the applications this is a good solution.</p>
<p>Lets look at the following code:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">object</span> m_locker = <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum3" style="color: #606060">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 50; i++)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum6" style="color: #606060">   6:</span>         <span style="color: #0000ff">int</span> iLocal = i;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum7" style="color: #606060">   7:</span>         Task.Factory.StartNew(() =&gt;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum8" style="color: #606060">   8:</span>                                   {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum9" style="color: #606060">   9:</span>                                       <span style="color: #0000ff">lock</span> (m_locker)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum10" style="color: #606060">  10:</span>                                       {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum11" style="color: #606060">  11:</span>                                           Console.WriteLine(iLocal);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum12" style="color: #606060">  12:</span>                                       }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum13" style="color: #606060">  13:</span>                                   });</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum14" style="color: #606060">  14:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span id="lnum15" style="color: #606060">  15:</span>     Console.ReadLine();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: #f4f4f4;"><span id="lnum16" style="color: #606060">  16:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>What do you think will be printed to the screen? If you though we are going to see the numbers 0-49 in order you are very wrong…</p>
<p>Here is one of the results of running this code:</p>
<p><a href="http://dev102.com/Dev102/wp-content/uploads/2012/02/lock.png"><img title="Lock" style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" src="http://dev102.com/Dev102/wp-content/uploads/2012/02/lock-thumb.png" border="0" alt="Lock" width="543" height="516" /></a></p>
<p>Chaos!</p>
<p>Look at 3 its almost at the end! while 10-30 are neatly ordered.</p>
<p>This is due to the fact that lock() does not keep order, you never know which thread will get the priority to work.</p>
<p>Now that we know not to count on lock to keep our threads in line there is one more thing to notice here.</p>
<p>This test was with 50 calls. What if this was a web server that handles many calls a second 24/7? Since lock() does not offer any promise of order, there is a chance that some of the threads will stay “stuck” in that lock statement for a long time and possibly forever! In many applications this is not a problem but sometimes it means there is a desperate user on the other side trying to understand what went wrong…</p>
<p>So in conclusion :</p>
<ol>
<li>Don’t count on the lock() statement to keep your threads in order</li>
<li>The lock() statement is not good enough for heavily multithreaded critical sections.</li>
</ol>
<p>Did this ever cause any trouble to you? if so please comment.</p>
<p>Thanks</p>
<p>Amit</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=O9QxcjlglNg:fWrlmf2M_FQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=O9QxcjlglNg:fWrlmf2M_FQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=O9QxcjlglNg:fWrlmf2M_FQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=O9QxcjlglNg:fWrlmf2M_FQ:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/O9QxcjlglNg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2012/02/26/2-important-things-to-notice-when-using-lock-statement/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2012/02/26/2-important-things-to-notice-when-using-lock-statement/</feedburner:origLink></item>
		<item>
		<title>How to Suppress / Disable Script Errors in WPF WebBrowser Control</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/wpRMG8rV5qQ/</link>
		<comments>http://www.dev102.com/2012/02/23/how-to-suppress-disable-script-errors-in-wpf-webbrowser-control/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 09:32:00 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[Script Errors]]></category>

		<category><![CDATA[ScriptElement]]></category>

		<category><![CDATA[WebBrowser]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1297</guid>
		<description><![CDATA[<p>&#160;</p>
<p>Hi </p>
<p>&#160;</p>
<p>I you worked with the WebBrowser control you probably met the following popup:</p>
<p>&#160;</p>
<p style="align: center"><a href="http://dev102.com/Dev102/wp-content/uploads/2012/02/script-error.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="script error" border="0" alt="script error" src="http://dev102.com/Dev102/wp-content/uploads/2012/02/script-error-thumb.png" width="561" height="361" /></a></p>
<p>&#160;</p>
<p>I have started to use the WPF WebBrowser control in some of my applications but when I tried to <a href="http://www.dev102.com/2007/12/22/how-to-suppress-disable-script-errors-in-webbrowser-control/">disable the script errors</a> I had a small problem the property WebBrowser.ScriptErrorsSuppressed&#160; was not there, bummer…</p>
<p>&#160;</p>
]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>Hi </p>
<p>&#160;</p>
<p>I you worked with the WebBrowser control you probably met the following popup:</p>
<p>&#160;</p>
<p style="align: center"><a href="http://dev102.com/Dev102/wp-content/uploads/2012/02/script-error.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="script error" border="0" alt="script error" src="http://dev102.com/Dev102/wp-content/uploads/2012/02/script-error-thumb.png" width="561" height="361" /></a></p>
<p>&#160;</p>
<p>I have started to use the WPF WebBrowser control in some of my applications but when I tried to <a href="http://www.dev102.com/2007/12/22/how-to-suppress-disable-script-errors-in-webbrowser-control/">disable the script errors</a> I had a small problem the property WebBrowser.ScriptErrorsSuppressed&#160; was not there, bummer…</p>
<p>&#160;</p>
<p>After some research I have come up with a new solution. It involves injecting each webpage we display a small piece of JavaScript which catches those errors and ignores them.</p>
<p>Here is how it is done:</p>
<p>This is the java script we are injecting:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">function</span> noError() </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> } </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> window.onerror = noError;</pre>
<p><!--CRLF--></div>
</div>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &#8220;Courier New&#8221;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &#8220;Courier New&#8221;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &#8220;Courier New&#8221;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>And now The way to inject the script to the WebBrowser control:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> InjectDisableErrorScript()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">var</span> doc = Browser.Document <span style="color: #0000ff">as</span> HTMLDocument;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">if</span> (doc != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>         <span style="color: #008000">//Create the sctipt element</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         <span style="color: #0000ff">var</span> scriptErrorSuppressed = (IHTMLScriptElement)doc.createElement(<span style="color: #006080">&quot;SCRIPT&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         scriptErrorSuppressed.type = <span style="color: #006080">&quot;text/javascript&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>         scriptErrorSuppressed.text = m_disableScriptError;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         <span style="color: #008000">//Inject it to the head of the page</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>         IHTMLElementCollection nodes = doc.getElementsByTagName(<span style="color: #006080">&quot;head&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         <span style="color: #0000ff">foreach</span> (IHTMLElement elem <span style="color: #0000ff">in</span> nodes)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>             <span style="color: #0000ff">var</span> head = (HTMLHeadElement)elem;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>             head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Don’t forget to do this every time before you navigate.</p>
<p>That’s it! Now you are script error free.</p>
<p>&#160;</p>
<p>Enjoy</p>
<p>&#160;</p>
<p>Amit</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=wpRMG8rV5qQ:17ujbr1QS4M:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=wpRMG8rV5qQ:17ujbr1QS4M:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=wpRMG8rV5qQ:17ujbr1QS4M:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=wpRMG8rV5qQ:17ujbr1QS4M:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/wpRMG8rV5qQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2012/02/23/how-to-suppress-disable-script-errors-in-wpf-webbrowser-control/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2012/02/23/how-to-suppress-disable-script-errors-in-wpf-webbrowser-control/</feedburner:origLink></item>
		<item>
		<title>Pay Attention to the Foreach Implicit Casting</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/0pAgNUkS7js/</link>
		<comments>http://www.dev102.com/2009/07/28/pay-attention-to-the-foreach-implicit-casting/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 16:11:22 +0000</pubDate>
		<dc:creator>moshe</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[cast]]></category>

		<category><![CDATA[casting]]></category>

		<category><![CDATA[Explicit]]></category>

		<category><![CDATA[foreach]]></category>

		<category><![CDATA[Implicit]]></category>

		<category><![CDATA[Object]]></category>

		<category><![CDATA[var]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1287</guid>
		<description><![CDATA[<p>Last week, I stumbled upon the following piece of code:  <p>&#160;</p> <div><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">IList&#60;<span style="color: #0000ff">double</span>&#62; doubles = <span style="color: #0000ff">new</span> List&#60;<span style="color: #0000ff">double</span>&#62; {22.123, 400.12, 100.22};
<span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> number <span style="color: #0000ff">in</span> doubles)
{
    Console.WriteLine(<span style="color: #006080">"The Current Number Is : {0}"</span>, number);
}
</pre></div>
<p>Can you see the bug in that code? We convert all of the doubles in the list to integers, truncating them. I was very surprised that the compiler didn't warn me about this issue, especially because the compiler would not let me write this code: 
<p>&#160;</p>
]]></description>
			<content:encoded><![CDATA[<p>Last week, I stumbled upon the following piece of code:</p>
<p>&nbsp;</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">IList&lt;<span style="color: #0000ff">double</span>&gt; doubles = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">double</span>&gt; {22.123, 400.12, 100.22};
<span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> number <span style="color: #0000ff">in</span> doubles)
{
    Console.WriteLine(<span style="color: #006080">"The Current Number Is : {0}"</span>, number);
}
</pre>
</div>
<p>Can you see the bug in that code? We convert all of the doubles in the list to integers, truncating them. I was very surprised that the compiler didn&#8217;t warn me about this issue, especially because the compiler would not let me write this code: </p>
<p>&nbsp;</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">double</span> someDouble = 22.523;
<span style="color: #0000ff">int</span> someInt = someDouble;
</pre>
</div>
<p>An explicit conversion is necessary here: </p>
<p>&nbsp;</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">int</span> someInt = (<span style="color: #0000ff">int</span>)someDouble;</pre>
</div>
<p>What is the difference between those two cases? Can&#8217;t I expect a uniform behavior from the compiler? The reason is probably historic - before C# 2.0 which introduced us with <i>Generics</i>, all of the containers held their items as <i>objects</i>. If the <i>foreach</i> statement didn&#8217;t automatically do the conversion, the user would have to explicitly cast every item in the collection from <i>object</i>… </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">ArrayList array = <span style="color: #0000ff">new</span> ArrayList {<span style="color: #006080">"moshe"</span>, <span style="color: #006080">"levi"</span>};
<span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> obj <span style="color: #0000ff">in</span> array)
{
    <span style="color: #0000ff">string</span> str = obj <span style="color: #0000ff">as</span> <span style="color: #0000ff">string</span>;
    <span style="color: #0000ff">if</span> (str != <span style="color: #0000ff">null</span>)
       Console.WriteLine(<span style="color: #006080">"The Current element is {0}"</span>, str);
}
</pre>
</div>
<p>In most cases, the collection items are of the same type so in order to reduce the explicit conversion effort, the C# developers decided to do an automatic conversion for us. If we test the MSIL which is generated for the <i>foreach</i> statement, we can clearly notice to the &#8220;injected&#8221; code which is responsible for the conversion: </p>
<p>&nbsp;</p>
<p align="center"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="clip_image002" align="center" src="http://dev102.com/Dev102/wp-content/uploads/2009/07/clip-image002-thumb.jpg" width="595" height="164"></a></p>
<p>Now that we understand what happens, here is a way to avoid those kinds of bugs. Using the <i>var</i> keyword, we can ask the compiler to define the type of the collection item according to its value. Here it is:</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">IList&lt;<span style="color: #0000ff">double</span>&gt; doubles = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">double</span>&gt; {22.123, 400.12, 100.22};
<span style="color: #0000ff">foreach</span> (var number <span style="color: #0000ff">in</span> doubles)
{
    Console.WriteLine(<span style="color: #006080">"The Current Number Is : {0}"</span>, number);
}
</pre>
</div>
<p>By changing this code, the compiler is responsible for determining the type of &#8220;number&#8221;. In most cases, that is exactly what we want…</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0pAgNUkS7js:98yf2DSPgig:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0pAgNUkS7js:98yf2DSPgig:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0pAgNUkS7js:98yf2DSPgig:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0pAgNUkS7js:98yf2DSPgig:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/0pAgNUkS7js" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/07/28/pay-attention-to-the-foreach-implicit-casting/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/07/28/pay-attention-to-the-foreach-implicit-casting/</feedburner:origLink></item>
		<item>
		<title>Changing Brush Brightness in WPF/Silverlight</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/hSCm12K0IoI/</link>
		<comments>http://www.dev102.com/2009/07/23/changing-brush-brightness-in-wpfsilverlight/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 15:30:20 +0000</pubDate>
		<dc:creator>ailon</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Silverlight]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[Brush Brightness]]></category>

		<category><![CDATA[GradientBrush]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1261</guid>
		<description><![CDATA[<p>Suppose you have several objects in your WPF application filled with similar Brushes which differ in brightness only. Like in this image:</p>

<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/07/red-rect.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="red_rect" border="0" alt="red_rect" src="http://dev102.com/Dev102/wp-content/uploads/2009/07/red-rect.png" width="244" height="175" /></a> </p>

<p align="center">&#160;</p>

<p>The nine rectangles use the same RadialGradienBrush but each rectangle is a little darker than previous. You can create these 9 different brushes in your favorite design tool (Expression Blend, etc.). But what if you need to change the base color later or you just need to make the brush user-configurable? Like in these samples:</p>]]></description>
			<content:encoded><![CDATA[<p>Suppose you have several objects in your WPF application filled with similar Brushes which differ in brightness only. Like in this image:</p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/07/red-rect.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="red_rect" src="http://dev102.com/Dev102/wp-content/uploads/2009/07/red-rect-thumb.png" border="0" alt="red_rect" width="244" height="175" /></a></p>
<p align="center"> </p>
<p>The nine rectangles use the same RadialGradienBrush but each rectangle is a little darker than previous. You can create these 9 different brushes in your favorite design tool (<a href="http://www.dev102.com/2007/12/22/how-to-use-microsoft-expression-blend-to-modify-a-control/">Expression Blend</a>, etc.). But what if you need to change the base color later or you just need to make the brush user-configurable? Like in these samples:</p>
<p> </p>
<p align="center"> <a href="http://dev102.com/Dev102/wp-content/uploads/2009/07/green-rect.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="green_rect" src="http://dev102.com/Dev102/wp-content/uploads/2009/07/green-rect-thumb.png" border="0" alt="green_rect" width="244" height="175" /></a></p>
<p> </p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/07/blue-rect.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="blue_rect" src="http://dev102.com/Dev102/wp-content/uploads/2009/07/blue-rect-thumb.png" border="0" alt="blue_rect" width="244" height="175" /></a></p>
<p align="center"> </p>
<p>The better approach would be to define the base brush once and then change it&#8217;s brightness in code. Unfortunately there&#8217;s no built-in way to change brightness of the Brush or Color in WPF. This is what we are going to address in this article.</p>
<p> </p>
<h3><span style="text-decoration: underline;">Hue, Saturation, Brightness</span></h3>
<p>System.Windows.Media.Color structure represents colors as 4 channels: alpha (<a href="http://www.dev102.com/2009/02/26/creating-a-wpf-silverlight-control-with-a-semi-transparent-background/">opacity</a>) channel and 3 channels for each core color (red, green and blue). aRGB color space is good for representing colors but it&#8217;s quite problematic to control such properties as brightness with these 4 channels. We will convert our colors to and from a <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/HSV_color_space">HSB color space</a></span> which is good at exactly what we need – controlling such perceptual color characteristics as hue, saturation and brightness. To do this we create a new class called HSBColor defined like this:</p>
<div>
<div style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> HSBColor</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> H { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> S { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> B { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">byte</span> A { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> HSBColor FromColor(Color rgbColor)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         HSBColor result = <span style="color: #0000ff">new</span> HSBColor();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>         <span style="color: #008000">// preserve alpha</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>         result.A = rgbColor.A;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>         <span style="color: #008000">// convert R, G, B to numbers from 0 to 1</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>         <span style="color: #0000ff">double</span> r = rgbColor.R / 255d;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>         <span style="color: #0000ff">double</span> g = rgbColor.G / 255d;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>         <span style="color: #0000ff">double</span> b = rgbColor.B / 255d;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span>         <span style="color: #0000ff">double</span> max = Math.Max(r, Math.Max(g, b));</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span>         <span style="color: #0000ff">double</span> min = Math.Min(r, Math.Min(g, b));</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span>         <span style="color: #008000">// hue</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span>         <span style="color: #0000ff">if</span> (max == min)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span>             result.H = 0;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>         <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (max == r)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>             result.H = (60 * (g - b) / (max - min) + 360) % 360;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span>         <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (max == g)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span>             result.H = 60 * (b - r) / (max - min) + 120;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>         <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span>             result.H = 60 * (r - g) / (max - min) + 240;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  32:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  33:</span>         <span style="color: #008000">// saturation</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  34:</span>         <span style="color: #0000ff">if</span> (max == 0)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  35:</span>             result.S = 0;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  36:</span>         <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  37:</span>             result.S = 1 - min / max;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  38:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  39:</span>         <span style="color: #008000">// brightness</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  40:</span>         result.B = max;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  41:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  42:</span>         <span style="color: #0000ff">return</span> result;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  43:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  44:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  45:</span>     <span style="color: #0000ff">public</span> Color ToColor()</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  46:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  47:</span>         Color result = <span style="color: #0000ff">new</span> Color();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  48:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  49:</span>         result.A = <span style="color: #0000ff">this</span>.A;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  50:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  51:</span>         <span style="color: #0000ff">int</span> hi = (<span style="color: #0000ff">int</span>)Math.Floor(<span style="color: #0000ff">this</span>.H / 60) % 6;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  52:</span>         <span style="color: #0000ff">double</span> f = <span style="color: #0000ff">this</span>.H / 60 - Math.Floor(<span style="color: #0000ff">this</span>.H / 60);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  53:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  54:</span>         <span style="color: #0000ff">double</span> p = <span style="color: #0000ff">this</span>.B * (1 - <span style="color: #0000ff">this</span>.S);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  55:</span>         <span style="color: #0000ff">double</span> q = <span style="color: #0000ff">this</span>.B * (1 - f * <span style="color: #0000ff">this</span>.S);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  56:</span>         <span style="color: #0000ff">double</span> t = <span style="color: #0000ff">this</span>.B * (1 - (1 - f) * <span style="color: #0000ff">this</span>.S);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  57:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  58:</span>         <span style="color: #0000ff">switch</span> (hi)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  59:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  60:</span>             <span style="color: #0000ff">case</span> 0:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  61:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  62:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(t * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  63:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  64:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  65:</span>             <span style="color: #0000ff">case</span> 1:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  66:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(q * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  67:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  68:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  69:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  70:</span>             <span style="color: #0000ff">case</span> 2:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  71:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  72:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  73:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(t * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  74:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  75:</span>             <span style="color: #0000ff">case</span> 3:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  76:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  77:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(q * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  78:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  79:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  80:</span>             <span style="color: #0000ff">case</span> 4:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  81:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(t * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  82:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  83:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  84:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  85:</span>             <span style="color: #0000ff">case</span> 5:</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  86:</span>                 result.R = (<span style="color: #0000ff">byte</span>)(<span style="color: #0000ff">this</span>.B * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  87:</span>                 result.G = (<span style="color: #0000ff">byte</span>)(p * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  88:</span>                 result.B = (<span style="color: #0000ff">byte</span>)(q * 255);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  89:</span>                 <span style="color: #0000ff">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  90:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  91:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  92:</span>         <span style="color: #0000ff">return</span> result;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  93:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  94:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The algorithm behind these conversions could be found in <span style="text-decoration: underline;"><a href="http://en.wikipedia.org/wiki/HSV_color_space">this Wikipedia article</a></span>.</p>
<p>Now all we need to do is modify brightness of every color used in our brushes.</p>
<h3> </h3>
<h3><span style="text-decoration: underline;">SetBrightness() Extension Method</span></h3>
<p>We will create an extension method for Brush class which creates a new brush based on the original brush but with new brightness applied. Our method will support all Color based brushes such as SolidColorBrush, LinearGradientBrush and RadialGradientBrush. For all other brush types our method will just return the clone of the original brush.</p>
<p>With SolidColorBrush we are going to set brightness on the color set in it&#8217;s Color property and for GradientBrush(es) – on Color property of each GradientStop. So, enough talking – here&#8217;s the code:</p>
<div>
<div style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Brush SetBrightness(<span style="color: #0000ff">this</span> Brush original, <span style="color: #0000ff">double</span> brightness)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> (brightness &lt; 0 || brightness &gt; 1)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<span style="color: #606060">   4:</span>         <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArgumentOutOfRangeException(<span style="color: #006080">"brightness"</span>,

              <span style="color: #006080">"brightness should be between 0 and 1"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     Brush result;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">if</span> (original <span style="color: #0000ff">is</span> SolidColorBrush)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         HSBColor hsb = HSBColor.FromColor(((SolidColorBrush)original).Color);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>         hsb.B = brightness;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>         result = <span style="color: #0000ff">new</span> SolidColorBrush(hsb.ToColor());</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>     <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (original <span style="color: #0000ff">is</span> GradientBrush)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>         result = original.Clone();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>         <span style="color: #008000">// change brightness of every gradient stop</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>         <span style="color: #0000ff">foreach</span> (GradientStop gs <span style="color: #0000ff">in</span> ((GradientBrush)result).GradientStops)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span>             HSBColor hsb = HSBColor.FromColor(gs.Color);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span>             hsb.B = brightness;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span>             gs.Color = hsb.ToColor();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span>     <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>         result = original.Clone();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>     <span style="color: #0000ff">return</span> result;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span> }</pre>
<p><!--CRLF--></div>
</div>
<h3> </h3>
<h3><span style="text-decoration: underline;">Using the Method</span></h3>
<p>Let&#8217;s look at a simple usage scenario. We will create those 9 rectangles pictured above. The window&#8217;s XAML looks like this:</p>
<div>
<div style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window</span> <span style="color: #ff0000">x:Class</span><span style="color: #0000ff">="Brightness.Window2"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #ff0000">xmlns:x</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     <span style="color: #ff0000">Title</span><span style="color: #0000ff">="Window2"</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">="250"</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">="300"</span> <span style="color: #ff0000">Background</span><span style="color: #0000ff">="#333333"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #ff0000">Loaded</span><span style="color: #0000ff">="Window_Loaded"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window.Resources</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Style</span> <span style="color: #ff0000">x:Key</span><span style="color: #0000ff">="RectangleStyle"</span> <span style="color: #ff0000">TargetType</span><span style="color: #0000ff">="Rectangle"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">Setter</span> <span style="color: #ff0000">Property</span><span style="color: #0000ff">="RadiusX"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="10"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">Setter</span> <span style="color: #ff0000">Property</span><span style="color: #0000ff">="RadiusY"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="10"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">Setter</span> <span style="color: #ff0000">Property</span><span style="color: #0000ff">="Margin"</span> <span style="color: #ff0000">Value</span><span style="color: #0000ff">="5"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Style</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window.Resources</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">UniformGrid</span> <span style="color: #ff0000">x:Name</span><span style="color: #0000ff">="mainGrid"</span> <span style="color: #ff0000">Margin</span><span style="color: #0000ff">="30"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle.Fill</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">RadialGradientBrush</span> <span style="color: #ff0000">GradientOrigin</span><span style="color: #0000ff">="-0.3 -0.3"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">GradientStop</span> <span style="color: #ff0000">Color</span><span style="color: #0000ff">="#ffffff"</span> <span style="color: #ff0000">Offset</span><span style="color: #0000ff">="0"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>                     <span style="color: #0000ff">&lt;</span><span style="color: #800000">GradientStop</span> <span style="color: #ff0000">Color</span><span style="color: #0000ff">="#0000FF"</span> <span style="color: #ff0000">Offset</span><span style="color: #0000ff">="1"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>                 <span style="color: #0000ff">&lt;/</span><span style="color: #800000">RadialGradientBrush</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  20:</span>             <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Rectangle.Fill</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  21:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Rectangle</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  22:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  23:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  24:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  25:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  26:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  27:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  28:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  29:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rectangle</span> <span style="color: #ff0000">Style</span><span style="color: #0000ff">="{StaticResource RectangleStyle}"</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  30:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">UniformGrid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  31:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>The first rectangle has it&#8217;s Fill property set to a RadialGradientBrush and all other rectangles have no fill at all. In the Window_Loaded event handler we just take the brush of the first rectangle and apply it to all other rectangles with different brightness.</p>
<div>
<div style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> Window_Loaded(<span style="color: #0000ff">object</span> sender, RoutedEventArgs e)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     Brush mainBrush = (mainGrid.Children[0] <span style="color: #0000ff">as</span> Rectangle).Fill;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 1; i &lt; 9; i++)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<span style="color: #606060">   6:</span>         (mainGrid.Children[i] <span style="color: #0000ff">as</span> Rectangle).Fill = 

               mainBrush.SetBrightness(1 - i / 10d);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>And that&#8217;s it.</p>
<p> </p>
<h3><span style="text-decoration: underline;">Final thoughts</span></h3>
<p>Rather than creating an extension method it would be nice to create a custom Brush that would take another “standard” brush and apply specified brightness to it. This would allow animation of Brightness property resulting in nice fade-in and out effects (for example). Unfortunately Microsoft has implemented abstract Brush class in such a way that no 3rd party can inherit from it. Some of the abstract methods in the Brush class are also declared as internal so there&#8217;s no way we can implement them. I hope they had a good reason for doing this. Otherwise it doesn&#8217;t make much sense.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=hSCm12K0IoI:F64F8kYEJ14:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=hSCm12K0IoI:F64F8kYEJ14:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=hSCm12K0IoI:F64F8kYEJ14:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=hSCm12K0IoI:F64F8kYEJ14:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/hSCm12K0IoI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/07/23/changing-brush-brightness-in-wpfsilverlight/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/07/23/changing-brush-brightness-in-wpfsilverlight/</feedburner:origLink></item>
		<item>
		<title>Creating A RESTful API With WCF</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/9gJABXUaY6M/</link>
		<comments>http://www.dev102.com/2009/07/17/creating-a-restful-api-with-wcf/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 05:21:46 +0000</pubDate>
		<dc:creator>Justin</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[REST]]></category>

		<category><![CDATA[RESTful]]></category>

		<category><![CDATA[Service]]></category>

		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1254</guid>
		<description><![CDATA[<h3><u>What is REST?</u></h3>  <p><strong></strong></p>  <p><strong></strong></p>  <p>Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. As such, it is not just a method for building &#34;web services.&#34; The terms &#34;representational state transfer&#34; and &#34;REST&#34; were introduced in 2000 in the doctoral dissertation of Roy Fielding,[1] one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification.</p>  <p>REST is a method of managing resources over HTTP. There are a number of methods of interacting with resources via REST.</p>  <table border="1" cellspacing="0" cellpadding="3"><tbody>     <tr>       <td width="50%"><strong>HTTP</strong></td>        <td width="50%"><strong>CRUD</strong></td>     </tr>      <tr>       <td width="50%">POST</td>        <td width="50%">Create</td>     </tr>      <tr>       <td width="50%">GET</td>        <td width="50%">Read</td>     </tr>      <tr>       <td width="50%">PUT</td>        <td width="50%">Update, Create</td>     </tr>      <tr>       <td width="50%">DELETE</td>        <td width="50%">Delete</td>     </tr>   </tbody></table>  <p>Systems which follow Fielding's REST principles are often referred to as &#34;RESTful&#34;.</p>  <p>&#160;</p>  <p><strong></strong></p>]]></description>
			<content:encoded><![CDATA[<h3><u>What is REST?</u></h3>
<p><strong></strong></p>
<p><strong></strong></p>
<p>Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. As such, it is not just a method for building &quot;web services.&quot; The terms &quot;representational state transfer&quot; and &quot;REST&quot; were introduced in 2000 in the doctoral dissertation of Roy Fielding,[1] one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification.</p>
<p>REST is a method of managing resources over HTTP. There are a number of methods of interacting with resources via REST.</p>
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="50%"><strong>HTTP</strong></td>
<td width="50%"><strong>CRUD</strong></td>
</tr>
<tr>
<td width="50%">POST</td>
<td width="50%">Create</td>
</tr>
<tr>
<td width="50%">GET</td>
<td width="50%">Read</td>
</tr>
<tr>
<td width="50%">PUT</td>
<td width="50%">Update, Create</td>
</tr>
<tr>
<td width="50%">DELETE</td>
<td width="50%">Delete</td>
</tr>
</tbody>
</table>
<p>Systems which follow Fielding&#8217;s REST principles are often referred to as &quot;RESTful&quot;.</p>
<p>&#160;</p>
<p><strong></strong></p>
<h3><u>Our Basic Service</u></h3>
<p>For the purpose of this post we are going to create a service that returns a product based on the requested product id.</p>
<p>Start by adding a new WCF service to your project. We&#8217;ll call it Products.</p>
<p>Change the interface that is generated to look like this:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> [ServiceContract]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProducts </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     [OperationContract]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     [WebInvoke(Method = <span style="color: #006080">&quot;GET&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>      ResponseFormat = WebMessageFormat.Json,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>      BodyStyle = WebMessageBodyStyle.Wrapped,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>      UriTemplate = <span style="color: #006080">&quot;get/{id}&quot;</span>)]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     <span style="color: #0000ff">string</span> Get(<span style="color: #0000ff">string</span> id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>In the above code we use the ServiceContract attribute to say that this interface is defining a service and makes the interface visible to WCF clients. We then define a method called Get. We mark this method with 2 attributes. First we use OperationContract. This just lets WCF know that this is a method of the service. Next we use WebInvoke. This means that the method is an invoke operation and maps it to an HTTP verb. In this case we have mapped it to the GET verb, specified we want our response to be JSON, and defined what to an URI will look like. The UriTemplate maps our id value to out method parameter.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Products : IProducts </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Get(<span style="color: #0000ff">string</span> id) </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>         <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;You requested product &quot;</span> + id;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This next part is simple. We are just implementing our interface. Our method is pretty simple for demonstration purposes. We&#8217;ll just output the id value that we receive.</p>
<p>This next part that you need in your Web.config is important for your service to work. This maps the incoming requests to your services.</p>
<p>&#160;</p>
<p><strong></strong></p>
<h3><u>Calling Our API</u></h3>
<p>So, now that our service is finished it is easy to call like http://yourdomain.com/Products.svc/get/123456. This is telling our service we want to use the Get method of the Products service and we are looking for product id 123456.</p>
<p>This will give us a response of:</p>
<p>{&quot;GetResult&quot;:&quot;&quot;You requested product 123456&quot;}</p>
<p>&#160;</p>
<p><strong></strong></p>
<h3><u>Wrap Up</u></h3>
<p>This is just a simple example of how to setup a RESTful web service quickly and easily by letting WCF take care of the plumbing and heavy lifting.</p>
<p>Stay tuned for more great posts from Dev102.com by grabbing the <a href="http://feeds.feedburner.com/Dev102feed">RSS feed</a>. You can also see more posts I have written on <a href="http://www.geekdaily.net">my blog</a> or grab my <a href="http://feeds.feedburner.com/GeekDaily">RSS feed</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=9gJABXUaY6M:cB5iwg7IgI4:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=9gJABXUaY6M:cB5iwg7IgI4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=9gJABXUaY6M:cB5iwg7IgI4:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=9gJABXUaY6M:cB5iwg7IgI4:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/9gJABXUaY6M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/07/17/creating-a-restful-api-with-wcf/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/07/17/creating-a-restful-api-with-wcf/</feedburner:origLink></item>
		<item>
		<title>We Are Back!</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/BIM5VU2im68/</link>
		<comments>http://www.dev102.com/2009/07/12/we-are-back/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 16:17:15 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.dev102.com/2009/07/12/we-are-back/</guid>
		<description><![CDATA[Hey all
&#160;
I am sorry for the long absence but I just got married and went on a great honeymoon, so I was not around! 
&#160;
I hope things will start to pick up from now.
&#160;
Just wanted to let you all know that we are back online, and to tell you that we have updated the Write [...]]]></description>
			<content:encoded><![CDATA[<p>Hey all</p>
<p>&#160;</p>
<p>I am sorry for the long absence but I just got married and went on a great honeymoon, so I was not around! </p>
<p>&#160;</p>
<p>I hope things will start to pick up from now.</p>
<p>&#160;</p>
<p>Just wanted to let you all know that we are back online, and to tell you that we have updated the <a href="http://www.dev102.com/write-for-dev102com/">Write Articles for Dev102.com</a> page with some new features.</p>
<ul>
<li>Payment has gone up to 35$!</li>
<li>We now have a free submission option where you can share articles and still be able to publish them elsewhere.</li>
</ul>
<p>So go ahead and check it out!</p>
<p>&#160;</p>
<p>Talk soon</p>
<p>&#160;</p>
<p>Amit.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=BIM5VU2im68:78elqnmB3yQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=BIM5VU2im68:78elqnmB3yQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=BIM5VU2im68:78elqnmB3yQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=BIM5VU2im68:78elqnmB3yQ:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/BIM5VU2im68" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/07/12/we-are-back/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/07/12/we-are-back/</feedburner:origLink></item>
		<item>
		<title>Formatting Your WPF Data – String.Format Is In the House!</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/0cVbBlkromE/</link>
		<comments>http://www.dev102.com/2009/05/28/formatting-your-wpf-data-stringformat-is-in-the-house/#comments</comments>
		<pubDate>Thu, 28 May 2009 12:32:30 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[.NET 3.5 SP1]]></category>

		<category><![CDATA[Binding]]></category>

		<category><![CDATA[property]]></category>

		<category><![CDATA[String.Format]]></category>

		<category><![CDATA[ValueConverter]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1226</guid>
		<description><![CDATA[<p>Hi</p>

<p>&#160;</p>

<p>When working with WPF I always found myself thinking how to handle Data formatting when a WPF control was bound to it. Let’s look at the following example of a window with a TextBlock that displays a DateTime:</p>

<div id="codeSnippetWrapper">
  <div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">&#60;</span><span style="color: #800000">Window</span> <span style="color: #ff0000">x:Class</span><span style="color: #0000ff">=&#34;BindingFormat.Window1&#34;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span>     <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">=&#34;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#34;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #ff0000">xmlns:x</span><span style="color: #0000ff">=&#34;http://schemas.microsoft.com/winfx/2006/xaml&#34;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #ff0000">Title</span><span style="color: #0000ff">=&#34;Window1&#34;</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">=&#34;300&#34;</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">=&#34;300&#34;</span><span style="color: #0000ff">&#62;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">&#60;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&#62;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span>         <span style="color: #0000ff">&#60;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">=&#34;{Binding}&#34;</span><span style="color: #0000ff">&#62;&#60;/</span><span style="color: #800000">TextBlock</span><span style="color: #0000ff">&#62;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">&#60;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&#62;</span></pre>
<!--CRLF-->

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> <span style="color: #0000ff">&#60;/</span><span style="color: #800000">Window</span><span style="color: #0000ff">&#62;</span></pre>
<!--CRLF--></div>
</div>]]></description>
			<content:encoded><![CDATA[<p>Hi</p>
<p> </p>
<p>When working with WPF I always found myself thinking how to handle Data formatting when a <a href="http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is-located-in-a-datatemplate/">WPF control </a>was bound to it. Let’s look at the following example of a window with a TextBlock that displays a DateTime:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window</span> <span style="color: #ff0000">x:Class</span><span style="color: #0000ff">="BindingFormat.Window1"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>     <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #ff0000">xmlns:x</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #ff0000">Title</span><span style="color: #0000ff">="Window1"</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">="300"</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">="300"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">="{Binding}"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">TextBlock</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p>And the code behind:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Window1()</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     InitializeComponent();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     DateTime d = DateTime.Now;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">this</span>.DataContext = d;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>Very Simple.</p>
<p> </p>
<p>The outcome is this:</p>
<p> </p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/bindingnoformat.png"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="Binding no Format" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/bindingnoformat-thumb.png" border="0" alt="Binding no Format" width="292" height="293" /></a></p>
<p>But what if we wanted to change the format of the DateTime? Before<a href="http://www.dev102.com/2009/05/04/4-grea-tips-for-custom-wpf-controls-developers/"> .NET 3.5 SP1 </a>we had 2 options:</p>
<p> </p>
<h3><span style="text-decoration: underline;">Adding a Property</span></h3>
<p>That was the easiest and fastest way to make the data in the UI formatted in the way you wanted let’s say we wanted the DateTime to display like this: 11:49 2/5/2009, we would have done this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Window1()</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     InitializeComponent();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">this</span>.DataContext = <span style="color: #0000ff">this</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Now</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     get</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         <span style="color: #0000ff">return</span> DateTime.Now.ToString(<span style="color: #006080">"HH:MM d/M/yyyy"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>and have the UI bind to the Now property:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window</span> <span style="color: #ff0000">x:Class</span><span style="color: #0000ff">="BindingFormat.Window1"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>     <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #ff0000">xmlns:x</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #ff0000">Title</span><span style="color: #0000ff">="Window1"</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">="300"</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">="300"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">="{Binding Now}"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">TextBlock</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p>The outcome is what we wanted:</p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/usingpropertytoformat.png"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="Using Property to format" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/usingpropertytoformat-thumb.png" border="0" alt="Using Property to format" width="292" height="293" /></a></p>
<p>I don&#8217;t like this way of work for 2 reasons:</p>
<ol>
<li>It ads clutter to the code behind and mixes data and UI elements because the Now property is strictly UI but it resides in the Data of the application.</li>
<li>There is no way to reuse it, we would have to duplicate the property anywhere we need to display this particular format.</li>
</ol>
<h3><span style="text-decoration: underline;">Using a ValueConverter</span></h3>
<p>This is a much better way of work because it solves the 2 problems I had with the Property method of formatting data. Using a converter keeps UI logic in the UI, removes the clutter from the code behind and is reusable.</p>
<p> </p>
<p>Here is how we do it:</p>
<p> </p>
<p>First we add the converter:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DateTimeConverter : IValueConverter</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #cc6633">#region</span> IValueConverter Members</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> Convert(<span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>, Type targetType, <span style="color: #0000ff">object</span> parameter</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>                         , System.Globalization.CultureInfo culture)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>         <span style="color: #0000ff">if</span> (<span style="color: #0000ff">value</span> <span style="color: #0000ff">is</span> DateTime)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>             DateTime d = (DateTime)<span style="color: #0000ff">value</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>             <span style="color: #0000ff">return</span> d.ToString(<span style="color: #006080">"HH:MM d/M/yyyy"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">string</span>.Empty;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> ConvertBack(<span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>, Type targetType, <span style="color: #0000ff">object</span> parameter</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>                             , System.Globalization.CultureInfo culture)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>         <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> NotImplementedException();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span> </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span>     <span style="color: #cc6633">#endregion</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>Use it with the binding:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window</span> <span style="color: #ff0000">x:Class</span><span style="color: #0000ff">="BindingFormat.Window1"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>     <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #ff0000">xmlns:x</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>         <span style="color: #ff0000">xmlns:local</span><span style="color: #0000ff">="clr-namespace:BindingFormat"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     <span style="color: #ff0000">Title</span><span style="color: #0000ff">="Window1"</span> <span style="color: #ff0000">Height</span><span style="color: #0000ff">="300"</span> <span style="color: #ff0000">Width</span><span style="color: #0000ff">="300"</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Window.Resources</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">local:DateTimeConverter</span> <span style="color: #ff0000">x:Key</span><span style="color: #0000ff">="BindingCVR"</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window.Resources</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">="{Binding Converter={StaticResource BindingCVR}}"</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Window</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p>And have the DataContext be DateTime.Now:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> Window1()</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>    InitializeComponent();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>    DateTime d = DateTime.Now;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>    <span style="color: #0000ff">this</span>.DataContext = d;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>And the outcome? Just as before:</p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/usingpropertytoformat.png"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="Using Property to format" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/usingpropertytoformat-thumb.png" border="0" alt="Using Property to format" width="292" height="293" /></a></p>
<p>The only drawback to this method is that it is takes more effort.</p>
<p> </p>
<p>But we just got lucky..</p>
<p> </p>
<h3><span style="text-decoration: underline;">Using Binding.StringFormat</span></h3>
<p>In .NET 3.5 SP1 we got the<a href="http://www.dev102.com/2008/04/16/how-to-use-aggregate-functions-with-linq-to-sql/"> StringFormat </a>option in Binding!</p>
<p> </p>
<p>This makes things so much easier All we have to do is have the Binding Expression look like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">="{Binding StringFormat='HH:MM d/M/yyyy'}"</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p>And that&#8217;s it, no extra Properties and ValueConverters.</p>
<p> </p>
<p>I could have copied the image again but trust me it gives the same outcome in a much more elegant way.</p>
<p> </p>
<p>Do you have a different way of work? let me know.</p>
<p> </p>
<p>Enjoy</p>
<p> </p>
<p>Amit</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0cVbBlkromE:A6e9ctC2NwY:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0cVbBlkromE:A6e9ctC2NwY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=0cVbBlkromE:A6e9ctC2NwY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=0cVbBlkromE:A6e9ctC2NwY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/0cVbBlkromE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/05/28/formatting-your-wpf-data-stringformat-is-in-the-house/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/05/28/formatting-your-wpf-data-stringformat-is-in-the-house/</feedburner:origLink></item>
		<item>
		<title>Creating Gapped and Bulleted Shapes in WPF/Silverlight</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/qr1x-HU0Isk/</link>
		<comments>http://www.dev102.com/2009/05/25/creating-gapped-and-bulleted-shapes-in-wpfsilverlight/#comments</comments>
		<pubDate>Mon, 25 May 2009 18:25:45 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[Silverlight]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[BulletedPath]]></category>

		<category><![CDATA[Bullets]]></category>

		<category><![CDATA[OpacityMask]]></category>

		<category><![CDATA[Shape]]></category>

		<guid isPermaLink="false">http://www.dev102.com/2009/05/25/creating-gapped-and-bulleted-shapes-in-wpfsilverlight/</guid>
		<description><![CDATA[<p><u></u></p>

<p>This article was written by<em> Alan Mendelevich</em></p>

<p><em></em></p>

<h3><u>The Problem</u></h3>

<p>Creating a shape with bullets on the joints of it's segments sounds like a really trivial task at a first glance. Just plaster some bullets on top of the shape. And it is really something like this until you decide you want to have transparent outlined bullets or opaque bullets with transparent outline around them. Like the ones in this picture:</p>

<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/bulleted-path-samples-thumb.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bulleted_path_samples" border="0" alt="bulleted_path_samples" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/bulleted-path-samples.png" width="331" height="319" /></a> </p>

<p align="center">&#160;</p>]]></description>
			<content:encoded><![CDATA[<p><u></u></p>
<p>This article was written by<em> Alan Mendelevich</em></p>
<p><em></em></p>
<p><em></em></p>
<h3><u>The Problem</u></h3>
<p>Creating a shape with bullets on the joints of it&#8217;s segments sounds like a really trivial task at a first glance. Just plaster some bullets on top of the shape. And it is really something like this until you decide you want to have transparent outlined bullets or opaque bullets with transparent outline around them. Like the ones in this picture:</p>
<p align="center"><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/bulleted-path-samples.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bulleted_path_samples" border="0" alt="bulleted_path_samples" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/bulleted-path-samples-thumb.png" width="331" height="319" /></a> </p>
<p align="center">&#160;</p>
<p>In raster scenarios this could be solved by simply deleting the pixels around the joint points but since we are talking about <a href="http://www.dev102.com/2008/09/25/wpf-binding-converter-best-practices-continued/">WPF</a> or <a href="http://www.dev102.com/2009/02/26/creating-a-wpf-silverlight-control-with-a-semi-transparent-background/">Silverlight</a> (vector graphics) this is not an option. </p>
<p>If you have a simplistic figure like the first rectangle in the above picture you may think about just creating shorter lines for it&#8217;s sides. This is not a very complicated task. However in the second (triangle) example calculating the end points for the lines gets a little more complicated. And try doing this without a masters degree in mathematics for the third and fourth (bottom) examples utilizing arcs and Bezier curves.</p>
<p>The second option is to cheat a little. You can use background color for bullet&#8217;s fill or outline. This would work well on a solid color background. But what if you have a gradient background (like in our sample picture), image background or if your bullet falls on the edge of some object or the shape itself?</p>
<p>I have faced this issue when working on a <u><a href="http://wpf.amcharts.com/line/" rel="nofollow">line chart</a></u> portion of <u><a href="http://wpf.amcharts.com/" rel="nofollow">amCharts for WPF</a></u> and here&#8217;s how I solved it.</p>
<p>&#160;</p>
<h3><u>The Solution</u></h3>
<p>The magic word for my solution is <u><a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.opacitymask.aspx" rel="nofollow">OpacityMask</a></u>. To quote MSDN documentation what it does is:</p>
<blockquote><p>Gets or sets an opacity mask, as a Brush implementation that is applied to any alpha-channel masking for the rendered content of this element.</p>
</blockquote>
<p>In other words it lets you apply different levels of opacity to different portions of your element.</p>
<p>So, the general idea is to create an opacity mask which has zero opacity spots in place where we want to put our bullets and 100% opacity everywhere else. Below are the main parts of the implementation along with some small but very important details.</p>
<ul>
<li>We derive our BulletedPath class from the abstract <u><a href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.aspx" rel="nofollow">Shape</a></u> class. <u><a href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.path.aspx" rel="nofollow">Path</a></u> could&#8217;ve been a better base class candidate, but unfortunately it&#8217;s sealed. </li>
<li>We add several bullet related dependency properties for controlling bullet shape, fill and outline. </li>
<li>We add a Data property which will store <u><a href="http://msdn.microsoft.com/en-us/library/system.windows.media.pathgeometry.aspx" rel="nofollow">PathGeometry</a></u> defining our shape. </li>
</ul>
<p>With all this done (you can see the implementation by downloading the source code for the control linked at the end of the article) we override OnRender method of our shape.</p>
<p>Here&#8217;s the method in it&#8217;s entirety and we&#8217;ll examine what it does below:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnRender(DrawingContext drawingContext)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     Pen shapePen = <span style="color: #0000ff">new</span> Pen(<span style="color: #0000ff">this</span>.Stroke, <span style="color: #0000ff">this</span>.StrokeThickness);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     shapePen.StartLineCap = <span style="color: #0000ff">this</span>.StrokeStartLineCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     shapePen.EndLineCap = <span style="color: #0000ff">this</span>.StrokeEndLineCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     shapePen.DashCap = <span style="color: #0000ff">this</span>.StrokeDashCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     shapePen.LineJoin = <span style="color: #0000ff">this</span>.StrokeLineJoin;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     shapePen.MiterLimit = <span style="color: #0000ff">this</span>.StrokeMiterLimit;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     shapePen.DashStyle = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         <span style="color: #0000ff">new</span> DashStyle(<span style="color: #0000ff">this</span>.StrokeDashArray, <span style="color: #0000ff">this</span>.StrokeDashOffset);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     <span style="color: #008000">// a create temporary drawing to find out </span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>     <span style="color: #008000">// real bounds of our shape using specified pen</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     GeometryDrawing maskBoundsDrawing = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         <span style="color: #0000ff">new</span> GeometryDrawing(Brushes.Black, shapePen, <span style="color: #0000ff">this</span>.Data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     Rect bounds = maskBoundsDrawing.Bounds;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     <span style="color: #008000">// create opacity mask for bullet holes</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>     GeometryGroup holes = <span style="color: #0000ff">this</span>.GetHoles();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>     RectangleGeometry maskRect = <span style="color: #0000ff">new</span> RectangleGeometry(bounds);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>     Geometry maskGeometry = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>         Geometry.Combine(maskRect, holes, GeometryCombineMode.Exclude, <span style="color: #0000ff">null</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     GeometryDrawing maskDrawing = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>         <span style="color: #0000ff">new</span> GeometryDrawing(Brushes.Black, <span style="color: #0000ff">null</span>, maskGeometry);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     DrawingBrush maskBrush = <span style="color: #0000ff">new</span> DrawingBrush(maskDrawing);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     maskBrush.Stretch = Stretch.None;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #008000">// apply opacity mask</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     drawingContext.PushOpacityMask(maskBrush);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #008000">// draw main shape</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     drawingContext.DrawGeometry(<span style="color: #0000ff">this</span>.Fill, shapePen, <span style="color: #0000ff">this</span>.Data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     drawingContext.Pop();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #008000">// draw bullets</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     RenderBullets(drawingContext);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span> }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>&#160; </pre>
<p><!--CRLF--></div>
</div>
<p>First we construct a pen to use for drawing the outline of our shape. Oddly enough Shape class doesn&#8217;t provide one property for setting the Pen. This is justified by the fact that it&#8217;s easier to set (and animate) individual properties of the pen on the whole shape object in <a href="http://www.dev102.com/2009/03/02/tidy-up-your-xaml-and-code-behind-use-usercontrols-inside-templates/">XAML</a> rather than creating a dedicated object for the outline pen.</p>
<p>This is understandable, however it would be really nice of Microsoft to provide a method to get a real Pen out of all these individual properties. This is a good candidate for an extension method, but that&#8217;s another story and for now we just construct the pen manually.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> Pen shapePen = <span style="color: #0000ff">new</span> Pen(<span style="color: #0000ff">this</span>.Stroke, <span style="color: #0000ff">this</span>.StrokeThickness);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> shapePen.StartLineCap = <span style="color: #0000ff">this</span>.StrokeStartLineCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> shapePen.EndLineCap = <span style="color: #0000ff">this</span>.StrokeEndLineCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> shapePen.DashCap = <span style="color: #0000ff">this</span>.StrokeDashCap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> shapePen.LineJoin = <span style="color: #0000ff">this</span>.StrokeLineJoin;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> shapePen.MiterLimit = <span style="color: #0000ff">this</span>.StrokeMiterLimit;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> shapePen.DashStyle = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         <span style="color: #0000ff">new</span> DashStyle(<span style="color: #0000ff">this</span>.StrokeDashArray, <span style="color: #0000ff">this</span>.StrokeDashOffset);</pre>
<p><!--CRLF--></div>
</div>
<p>Now comes a tricky part.</p>
<p>&#160;</p>
<p>We need to construct our opacity mask. Opacity mask is a Brush and it gets applied to the whole drawing area of the element. The size of that area depends not only on the geometry of our shape but on the size of it&#8217;s stroke, it&#8217;s miter, etc. For example if you have a line from point (0, 0) to (0, 100) and the StrokeThickness is set to 100 you&#8217;d get a line that (at the very least) occupies space from -50 to 50 on the Y-axis and it could be more dependent on the caps of that line and other settings. So, in order to get the final bounds of our shape before we actually draw it we create a temporary drawing using our actual pen settings and use it&#8217;s bounds as a rectangle area for our mask:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> GeometryDrawing maskBoundsDrawing = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #0000ff">new</span> GeometryDrawing(Brushes.Black, shapePen, <span style="color: #0000ff">this</span>.Data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> Rect bounds = maskBoundsDrawing.Bounds;</pre>
<p><!--CRLF--></div>
</div>
<p>Then we get the geometries for our bullet holes (see the source code for details) and exclude them from our rectangular area. This way we get a geometry which is a rectangle with a holes in it.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> RectangleGeometry maskRect = <span style="color: #0000ff">new</span> RectangleGeometry(bounds);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> Geometry maskGeometry = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     Geometry.Combine(maskRect, holes, GeometryCombineMode.Exclude, <span style="color: #0000ff">null</span>);</pre>
<p><!--CRLF--></div>
</div>
<p>Now we need to create a brush for our opacity mask from this geometry. </p>
<p>We create a GeometryDrawing using any opaque brush for it&#8217;s fill (Brushes.Black for example) and create a DrawingBrush from this drawing. It&#8217;s important to set brush&#8217;s Stretch property to None so it&#8217;s not distorted in any way.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> GeometryDrawing maskDrawing = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #0000ff">new</span> GeometryDrawing(Brushes.Black, <span style="color: #0000ff">null</span>, maskGeometry);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> DrawingBrush maskBrush = <span style="color: #0000ff">new</span> DrawingBrush(maskDrawing);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> maskBrush.Stretch = Stretch.None;</pre>
<p><!--CRLF--></div>
</div>
<p>And now all that&#8217;s left is to push our opacity mask on a DrawingContext, draw our shape (opacity mask will make sure that there are holes on the junction points), pop the mask and draw the bullets:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">// apply opacity mask</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> drawingContext.PushOpacityMask(maskBrush);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000">// draw main shape</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> drawingContext.DrawGeometry(<span style="color: #0000ff">this</span>.Fill, shapePen, <span style="color: #0000ff">this</span>.Data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> drawingContext.Pop();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000">// draw bullets</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> RenderBullets(drawingContext);</pre>
<p><!--CRLF--></div>
</div>
<p>And that&#8217;s it.</p>
<p>&#160;</p>
<p>I&#8217;ve omitted implementations of the other reasonably trivial tasks like drawing the bullets from the article but you can always take a look at the implementation by downloading the source code of BulletedPath control.</p>
<p>&#160;</p>
<p>You can download the binary control, C# source code and class reference from <u><a href="http://devblog.ailon.org/devblog/page/Downloads.aspx" rel="nofollow">download section of my blog</a></u>. It&#8217;s released under the <u><a href="http://www.opensource.org/licenses/bsd-license.php">BSD license</a></u> so feel free to use and improve it.</p>
<p>&#160;</p>
<p><i>About the author: Alan Mendelevich is a software developer from Vilnius, Lithuania. He has more than 10 years of professional software development experience for both Web and Windows. Currently he works as a lead developer on </i><u><a href="http://wpf.amcharts.com/"><i>amCharts for WPF</i></a></u><i> project – a charting control suite for WPF platform and </i><u><a href="http://spaweditor.com/"><i>SPAW Editor</i></a></u><i> – open source web based WYSIWYG HTML editor control.</i></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=qr1x-HU0Isk:sWagGWBQKFs:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=qr1x-HU0Isk:sWagGWBQKFs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=qr1x-HU0Isk:sWagGWBQKFs:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=qr1x-HU0Isk:sWagGWBQKFs:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/qr1x-HU0Isk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/05/25/creating-gapped-and-bulleted-shapes-in-wpfsilverlight/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/05/25/creating-gapped-and-bulleted-shapes-in-wpfsilverlight/</feedburner:origLink></item>
		<item>
		<title>Parameter count mismatch Error</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/u-ykFWvcZJw/</link>
		<comments>http://www.dev102.com/2009/05/19/parameter-count-mismatch-error/#comments</comments>
		<pubDate>Tue, 19 May 2009 10:34:59 +0000</pubDate>
		<dc:creator>Shahar Y</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[BeginInvoke]]></category>

		<category><![CDATA[Error]]></category>

		<category><![CDATA[exception]]></category>

		<category><![CDATA[Invoke]]></category>

		<category><![CDATA[mismatch]]></category>

		<category><![CDATA[parameters]]></category>

		<category><![CDATA[UI]]></category>

		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1239</guid>
		<description><![CDATA[<p>Let me share you with one of the weirdest errors I ever encountered. Recently, I have been working on a distributed application which is built from a server and some clients. The clients are Windows Forms applications. Yesterday, I spent a whole day chasing a very weird and strange error - I was getting an exception at the main method (<a href="http://www.dev102.com/2008/04/14/unhandled-exceptions-handle-them/">unhandled exception</a>) of the client application. Here is what I got:</p> <p><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/image.png" align="center"><img style="border-right-width: 0px; margin: 10px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/image-thumb.png" width="577" height="274"></a> </p> <p>The error description was:</p>]]></description>
			<content:encoded><![CDATA[<p>Let me share you with one of the weirdest errors I ever encountered. Recently, I have been working on a distributed application which is built from a server and some clients. The clients are Windows Forms applications. Yesterday, I spent a whole day chasing a very weird and strange error - I was getting an exception at the main method (<a href="http://www.dev102.com/2008/04/14/unhandled-exceptions-handle-them/">unhandled exception</a>) of the client application. Here is what I got:</p>
<p><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/image.png" align="center"><img style="border-right-width: 0px; margin: 10px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/image-thumb.png" width="577" height="274"></a> </p>
<p>The error description was: &#8220;<strong>Parameters count mismatch</strong>&#8220;. Well, it didn&#8217;t help me a lot, but I thought that looking at the stack trace might be a good idea. So, here it is:</p>
<p><a href="http://dev102.com/Dev102/wp-content/uploads/2009/05/image1.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://dev102.com/Dev102/wp-content/uploads/2009/05/image-thumb1.png" width="577" height="230"></a> </p>
<p>As you can see, there is nothing helpful here because the stack trace is full of .Net Framework methods, none of those methods are mine. Well after a whole day, trying to understand what happened, I figured out what was wrong. Take a look at the very simplified version of my code:</p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">delegate</span> <span style="color: #0000ff">void</span> ChangeColorEventHandler(<span style="color: #0000ff">object</span> sender, Color color);

<span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ChangeColor(<span style="color: #0000ff">object</span> sender, Color newColor)
{
    <span style="color: #0000ff">if</span>(InvokeRequired)
    {
        BeginInvoke(<span style="color: #0000ff">new</span> ChangeColorEventHandler(ChangeColor), newColor);
        <span style="color: #0000ff">return</span>;
    }
    BackColor = newColor;
}</pre>
</div>
<p>The ChangeColor method is changing the form back color on the UI thread. This method was called due to a server request, not because of a user action. Do you see what is wrong here? I didn&#8217;t set the sender as a parameter to the BeginInvoke method. That is it! It turns out that if you get this strange error, you probably called a BeginInvoke or Invoke methods with a wrong set of parameters. After changing my code to: </p>
<div>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ChangeColor(<span style="color: #0000ff">object</span> sender, Color newColor)
{
    <span style="color: #0000ff">if</span>(InvokeRequired)
    {
        BeginInvoke(<span style="color: #0000ff">new</span> ChangeColorEventHandler(ChangeColor), sender, newColor);
        <span style="color: #0000ff">return</span>;
    }
    BackColor = newColor;
}</pre>
</div>
<p>Everything was fine. Hope it would be helpful for some of you one day&#8230;</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=u-ykFWvcZJw:KK9CfmORMW8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=u-ykFWvcZJw:KK9CfmORMW8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=u-ykFWvcZJw:KK9CfmORMW8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=u-ykFWvcZJw:KK9CfmORMW8:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/u-ykFWvcZJw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/05/19/parameter-count-mismatch-error/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/05/19/parameter-count-mismatch-error/</feedburner:origLink></item>
		<item>
		<title>4 Great Tips for Custom WPF Controls Developers</title>
		<link>http://feedproxy.google.com/~r/Dev102feed/~3/20gxHnyJ31c/</link>
		<comments>http://www.dev102.com/2009/05/04/4-grea-tips-for-custom-wpf-controls-developers/#comments</comments>
		<pubDate>Mon, 04 May 2009 14:57:39 +0000</pubDate>
		<dc:creator>Amit</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[Attribute]]></category>

		<category><![CDATA[Blend]]></category>

		<category><![CDATA[Custom Controls]]></category>

		<category><![CDATA[design]]></category>

		<category><![CDATA[MetaData]]></category>

		<category><![CDATA[ToolBox]]></category>

		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.dev102.com/?p=1227</guid>
		<description><![CDATA[<p><em>This article was written by Alan Mendelevich</em> </p>

<p><em></em>&#160; </p>

<p>Hi </p>

<p>&#160; </p>

<p>Recently I've been working on a set of <a href="http://wpf.amcharts.com/">custom WPF controls</a>. </p>

<p>There are numerous good books and <a href="http://devblog.ailon.org/devblog/post/2009/04/Book-Review-Pro-WPF-in-C-2008-Windows-Presentation-Foundation-with-NET-352c-Second-Edition-by-Matthew-MacDonald.aspx">articles</a> about WPF in general and some basic information on custom control development but I have yet to find a good article (or book) with in-depth coverage of custom control development in general and design-time related issues in particular. </p>

<p>&#160; </p>]]></description>
			<content:encoded><![CDATA[<p><em>This article was written by Alan Mendelevich</em></p>
<p><em></em> </p>
<p>Hi</p>
<p> </p>
<p>Recently I&#8217;ve been working on a set of <a href="http://wpf.amcharts.com/">custom WPF controls</a>.</p>
<p>There are numerous good books and <a href="http://devblog.ailon.org/devblog/post/2009/04/Book-Review-Pro-WPF-in-C-2008-Windows-Presentation-Foundation-with-NET-352c-Second-Edition-by-Matthew-MacDonald.aspx">articles</a> about WPF in general and some basic information on custom control development but I have yet to find a good article (or book) with in-depth coverage of custom control development in general and design-time related issues in particular.</p>
<p> </p>
<p>I&#8217;ve done my share of custom ASP.NET control development (see ASP.NET Controls for amCharts <a href="http://www.amcharts.com/aspnet">http://www.amcharts.com/aspnet</a>, or SPAW Editor ASP.NET Edition <a href="http://spaweditor.com">http://spaweditor.com</a>), and actually the basic principles are not that different. However some things should be implemented in a totally different manner and it wasn&#8217;t really obvious to me where those differences start and what to do about them. I&#8217;ve spent quite some time figuring these things out, so I decided to share my findings here hoping that it would save someone some time which could be better spent implementing new features.</p>
<p> </p>
<p>This article covers several areas related to <a href="http://www.dev102.com/2008/05/09/6-useful-visual-studio-tweaks-you-need-to-know/">WPF Designer </a>related features/issues which are implemented using different means than you would use while building ASP.NET/WinForms controls.</p>
<p> </p>
<h3><span style="text-decoration: underline;">Metadata Store and Separate Metadata Assembly</span></h3>
<p>Some design-time related attributes like CategoryAttribute (which specifies grouping category in property grid) could be set on a property the “old-fashioned” way, but general approach has switched toward the <a href="http://msdn.microsoft.com/en-us/library/bb546923.aspx">Metadata Store</a>. Metadata store is a mechanism used by WPF Designer to decouple design-time metadata from implementation.</p>
<p> </p>
<p>Design-time metadata is deployed in separate specialized assemblies which should follow specific naming conventions in order for design tools to find and use them. You create an assembly with “.Design” appended to the main assembly name for general design-time metadata.</p>
<p> </p>
<p>For example if you have an implementation assembly called MyLibrary.dll then design-time metadata should reside in MyLibrary.Design.dll. Specialized <a href="http://www.dev102.com/2008/04/29/a-great-visual-studio-add-on-you-must-have/">Visual Studio </a>or Expression Blend metadata could be placed in additional MyLibrary.VisualStudio.Design.dll and MyLibrary.Expression.Design.dll.</p>
<p>In addition to general good design principles, separation of design-time metadata into specialized assemblies has one very practical implication, most of the new attribute classes are defined in the Microsoft.Windows.Design.dll. If you add a reference to this DLL to your implementation project users of your control will most likely get an exception like “Unable to install or run the application. The application requires that the assembly Microsoft.Windows.Design Version 3.5.0.0 be installed in the Global Assembly Cache (GAC) first. Since it&#8217;s not a part of normal .NET deployment runtime.</p>
<p>To create a metadata store, you create a new project, add a reference to Microsoft.Windows.Design.dll to it and create a class that implements IRegisterMetadata interface like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="font-size: 8pt; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">namespace</span>MyLibrary.Design</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span>MyLibraryMetadata : IRegisterMetadata</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Register()</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>AttributeTableBuilder builder = <span style="color: #0000ff">new</span>AttributeTableBuilder();</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>builder.AddCustomAttributes(<span style="color: #0000ff">typeof</span>(MyClass)</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>                                       ,<span style="color: #0000ff">new</span>ToolboxBrowsableAttribute(<span style="color: #0000ff">false</span>));</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>MetadataStore.AddAttributeTable(builder.CreateTable());</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span> }</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>The IRegisterMetadata interface requires you to implement only one method – Register(). In this method you create an instance of AttributeTableBuilder and add attributes to your classes, properties, methods, etc. one-by-one using AddCustomAttributes() method. Then you add this table to the MetadataStore.</p>
<p> </p>
<h3><span style="text-decoration: underline;">Toolbox visibility</span></h3>
<p>When you create a control library you will most likely create some classes deriving from Control class (or one of it&#8217;s derivatives), you don&#8217;t want then to show up in the Visual Studio toolbox along your main controls. In ASP.NET/WinForms you would use the ToolboxItem(false) attribute to achieve that. However the WPF designer pays no attention to this attribute. For the WPF designer you should use <a href="://msdn.microsoft.com/en-us/library/microsoft.windows.design.toolboxbrowsableattribute.aspx">ToolboxBrowsableAttribute</a>from the Microsoft.Windows.Design namespace. You attach it to your classes like this:</p>
<p> </p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="font-size: 8pt; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span>builder.AddCustomAttributes(<span style="color: #0000ff">typeof</span>(MyClass)</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>         , <span style="color: #0000ff">new</span>ToolboxBrowsableAttribute(<span style="color: #0000ff">false</span>));</pre>
<p><!--CRLF--></p>
</div>
</div>
<h4> </h4>
<h3><span style="text-decoration: underline;">Toolbox icon</span></h3>
<p>You can add toolbox icon for your controls using <a href="http://msdn.microsoft.com/en-us/library/microsoft.windows.design.thumbnailattribute.aspx">ThumbnailAttribute class</a>. However this attribute was only added in .NET 3.5 Service Pack 1. So, you have to make a choice here, supporting .NET 3.5 without SP1 and earlier versions of the .NET Framework, or specifying different thumbnails for Visual Studio and Expression Blend.</p>
<p>A simple and version agnostic method is to add images named according to the specific naming convention of your project. The convention is to name an image in a form of ClassName.Icon.ext. So, if you have a class named MyControl, then your icon should be named MyControl.Icon.png (for example).</p>
<p> </p>
<h3><span style="text-decoration: underline;">Collection editors</span></h3>
<p>In previous .NET designer technologies you had to create a class derived from System.ComponentModel.Design.CollectionEditor if you wanted to allow users to add objects of more than one type to collections through the collection editor in Visual Studio. The approach is demonstrated in this <a href="http://www.codeproject.com/KB/cs/dzcollectioneditor.aspx#CollectionEditor">CodeProject article</a>.</p>
<p> </p>
<p>In WPF this no longer works.</p>
<p> </p>
<p>But fortunately you can achieve the same result just by setting <a href="http://msdn.microsoft.com/en-us/library/microsoft.windows.design.propertyediting.newitemtypesattribute.aspx">NewItemTypesAttribute</a> on your property. This is how it&#8217;s done. Suppose you have a property defined as a collection of brushes like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="font-size: 8pt; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span>List&lt;Brush&gt; MyBrushes;</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>And you want users to be able to add SolidColorBrush and LinearGradientBrush objects to this collection via collection editor in Visual Studio. You do this by attaching NewItemTypesAttribute attribute in Register method of your metadata class like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="font-size: 8pt; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span>builder.AddCustomAttributes(<span style="color: #0000ff">typeof</span>(MyClass), <span style="color: #006080">"MyBrushes"</span>,</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>     <span style="color: #0000ff">new</span>NewItemTypesAttribute(<span style="color: #0000ff">typeof</span>(System.Windows.Media.SolidColorBrush),</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">typeof</span>(System.Windows.Media.LinearGradientBrush)));</pre>
<p><!--CRLF--></p>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; line-height: 12pt; font-family: 'Courier New', courier, monospace; background-color: #f4f4f4; text-align: left; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> </pre>
<p><!--CRLF--></p>
</div>
</div>
<p> </p>
<h3><span style="text-decoration: underline;">Conclusion</span></h3>
<p>This article touched only some of the issues which developers new to WPF control development may encounter. As WPF gains more and more popularity eventually someone will write a good book and/or tutorial on the subject. Maybe someone already did and I just wasn&#8217;t lucky to find it. If so, please, let me know by commenting.</p>
<p>However there&#8217;s pretty detailed coverage of WPF designer extensibility in this <a href="http://msdn.microsoft.com/en-us/library/bb546938.aspx">MSDN library section</a>  even though it&#8217;s is not written in a very narrative tutorial-like style.</p>
<p> </p>
<p>Alan.</p>
<p><em></em></p>
<p><em>As a bonus to this article Alan has included a 50% discount coupon for the WPF charting controls (</em><a href="http://wpf.amcharts.com/"><em>http://wpf.amcharts.com</em></a><em>) he has been working on lately. All you have to do is enter “dev102” (without quotes) when ordering it.</em></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:NT3aGUK8eJ0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=NT3aGUK8eJ0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=20gxHnyJ31c:g-fixkXRdhg:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=20gxHnyJ31c:g-fixkXRdhg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Dev102feed?i=20gxHnyJ31c:g-fixkXRdhg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Dev102feed?a=20gxHnyJ31c:g-fixkXRdhg:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/Dev102feed?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Dev102feed/~4/20gxHnyJ31c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.dev102.com/2009/05/04/4-grea-tips-for-custom-wpf-controls-developers/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.dev102.com/2009/05/04/4-grea-tips-for-custom-wpf-controls-developers/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 1.606 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-05-18 12:57:18 -->
