<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ira Lukhezo&#039;s blog</title>
	<atom:link href="http://lukhezo.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lukhezo.com</link>
	<description>.NET Haberdasher</description>
	<lastBuildDate>Wed, 11 Jan 2012 19:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='lukhezo.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ira Lukhezo&#039;s blog</title>
		<link>http://lukhezo.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://lukhezo.com/osd.xml" title="Ira Lukhezo&#039;s blog" />
	<atom:link rel='hub' href='http://lukhezo.com/?pushpress=hub'/>
		<item>
		<title>Get directory of an executing assembly in C# and Visual Basic</title>
		<link>http://lukhezo.com/2011/12/17/get-directory-of-an-executing-assembly-in-c-and-visual-basic/</link>
		<comments>http://lukhezo.com/2011/12/17/get-directory-of-an-executing-assembly-in-c-and-visual-basic/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 10:03:33 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[visual basic]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/?p=1185</guid>
		<description><![CDATA[Continuing my series of short but useful tips and tricks, one usually needs to find the directory of an executing assembly. The simple console application below shows how to do this. The Visual Basic example is after the C# one. C# using System; using System.IO; using System.Reflection; &#160; namespace ConsoleApplication2 { internal class Program { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1185&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Continuing my series of short but useful tips and tricks, one usually needs to find the directory of an executing assembly. The simple console application below shows how to do this. The Visual Basic example is after the C# one.</p>
<h3>C#</h3>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div><span style="color:#0000ff;">using</span> System.IO;</div>
<div><span style="color:#0000ff;">using</span> System.Reflection;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ConsoleApplication2</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Directory: &quot;</span> + AssemblyDirectory());</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Location: &quot;</span> + <span style="color:#2b91af;">Assembly</span>.GetExecutingAssembly().Location);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Console</span>.ReadKey();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> AssemblyDirectory()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> uri = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">UriBuilder</span>(<span style="color:#2b91af;">Assembly</span>.GetExecutingAssembly().CodeBase);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> <span style="color:#2b91af;">Path</span>.GetDirectoryName(<span style="color:#2b91af;">Uri</span>.UnescapeDataString(uri.Path));</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<h3>Visual Basic</h3>
<p>&#160;</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">Imports</span> System.Reflection</div>
<div><span style="color:#0000ff;">Imports</span> System.IO</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">Module</span> <span style="color:#2b91af;">Module1</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">Sub</span> Main()</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Directory: &quot;</span> + AssemblyDirectory())</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Location: &quot;</span> + <span style="color:#2b91af;">Assembly</span>.GetExecutingAssembly().Location)</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#2b91af;">Console</span>.ReadKey()</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">Public</span> <span style="color:#0000ff;">Function</span> AssemblyDirectory() <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">String</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">Dim</span> uri = <span style="color:#0000ff;">New</span> <span style="color:#2b91af;">UriBuilder</span>(<span style="color:#2b91af;">Assembly</span>.GetExecutingAssembly().CodeBase)</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">Return</span> <span style="color:#2b91af;">Path</span>.GetDirectoryName(System.<span style="color:#2b91af;">Uri</span>.UnescapeDataString(<span style="color:#2b91af;">uri</span>.Path))</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Function</span></div>
<div>&#160;</div>
<div><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Module</span></div>
<div></div>
</p></div>
<br />Filed under: <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a> Tagged: <a href='http://lukhezo.com/tag/assembly/'>assembly</a>, <a href='http://lukhezo.com/tag/c/'>C#</a>, <a href='http://lukhezo.com/tag/console/'>console</a>, <a href='http://lukhezo.com/tag/reflection/'>reflection</a>, <a href='http://lukhezo.com/tag/visual-basic/'>visual basic</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1185/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1185&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/12/17/get-directory-of-an-executing-assembly-in-c-and-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>
	</item>
		<item>
		<title>The calling thread must be STA, because many UI components require this</title>
		<link>http://lukhezo.com/2011/12/10/the-calling-thread-must-be-sta-because-many-ui-components-require-this-in-wpfmvvm/</link>
		<comments>http://lukhezo.com/2011/12/10/the-calling-thread-must-be-sta-because-many-ui-components-require-this-in-wpfmvvm/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 14:43:41 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/?p=1176</guid>
		<description><![CDATA[Whilst working on my current WPF application, I was at Sixes &#38; Sevens trying to correct the following error; Message: The calling thread must be STA, because many UI components require this I ended up hacking my way out of this problem a week or so ago, hacks however, make me feel extremely dirty, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1176&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Whilst working on my current WPF application, I was at Sixes &amp; Sevens trying to correct the following error; </p>
<p><strong>Message: The calling thread must be STA, because many UI components require this</strong></p>
<p>I ended up hacking my way out of this problem a week or so ago, hacks however, make me feel extremely dirty, and subscribing to the “leave code as you would like to find it” mantra meant one had to subsequently revisit it to “tidy-up”.&#160; I have a strictly MVVM application that is complex and multithreaded, needing to update UI components from web service calls that typically are asynchronous. The issue here was that I was using code like this in my view model <em>thinking</em> I had access to the dispatcher.</p>
<div style="font-family:monospace;">
<div style="text-indent:-4ex;margin-left:6ex;"><span style="color:#2b91af;">Dispatcher</span>.CurrentDispatcher.BeginInvoke((<span style="color:#2b91af;">Action</span>) (() =&gt;</div>
<div>&#160;</div>
</p></div>
<p>The mistake I made was in <em>not getting a reference to the correct dispatcher</em>.</p>
<p> To correct this issue, ensure you have a variable that gets a reference to the correct dispatcher when the view model is <em>instantiated</em>, you can then use this dispatcher in your view model, without having to pollute your code behind files.</p>
<div style="font-family:monospace;">
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">Dispatcher</span> dispatcher;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> DemoViewModel()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.dispatcher = <span style="color:#2b91af;">Dispatcher</span>.CurrentDispatcher;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div></div>
</p></div>
<br />Filed under: <a href='http://lukhezo.com/category/general/'>General</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1176/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1176&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/12/10/the-calling-thread-must-be-sta-because-many-ui-components-require-this-in-wpfmvvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>
	</item>
		<item>
		<title>Single instance WPF application in C#</title>
		<link>http://lukhezo.com/2011/12/05/single-instance-wpf-application-in-c/</link>
		<comments>http://lukhezo.com/2011/12/05/single-instance-wpf-application-in-c/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 18:47:36 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/?p=1160</guid>
		<description><![CDATA[Keeping in the same vein as my previous post, I frequently find that I have to solve the same problems at times. After a year or several months I usually move onto another project, consequently no longer have the source code, so I am posting this as a bit of a sticky as I am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1160&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Keeping in the same vein as my previous post, I frequently find that I have to solve the same problems at times. After a year or several months I usually move onto another project, consequently no longer have the source code, so I am posting this as a bit of a sticky as I am sure someone will find it useful.</p>
<p>Visual Basic has single instance classes one can use, one can import these into any C# application and consume them (that, after all,&#160; is the real beauty of .NET), but I found that there was too much code, and too many classes, and it was taking me too long a little while back. I solve this problem using the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx">Process</a> class. Create a WPF application and use as follows;</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System.Diagnostics;</div>
<div><span style="color:#0000ff;">using</span> System.Linq;</div>
<div><span style="color:#0000ff;">using</span> System.Windows;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> SingleInstanceApp</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Interaction logic for App.xaml</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">App</span> : <span style="color:#2b91af;">Application</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnStartup(<span style="color:#2b91af;">StartupEventArgs</span> e)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;">IsAppAlreadyRunning();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> IsAppAlreadyRunning()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Process</span> currentProcess = <span style="color:#2b91af;">Process</span>.GetCurrentProcess();</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (<span style="color:#2b91af;">Process</span>.GetProcessesByName(currentProcess.ProcessName).Any(p =&gt; p.Id != currentProcess.Id))</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">&quot;Another instance is already running.&quot;</span>, <span style="color:#a31515;">&quot;Application already running&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:36ex;"><span style="color:#2b91af;">MessageBoxButton</span>.OK, <span style="color:#2b91af;">MessageBoxImage</span>.Exclamation);</div>
<div style="text-indent:-4ex;margin-left:20ex;">Current.Shutdown();</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">return</span>;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div></div>
<div></div>
</p></div>
<br />Filed under: <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a>, <a href='http://lukhezo.com/category/wpf/'>WPF</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1160/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1160/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1160&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/12/05/single-instance-wpf-application-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>
	</item>
		<item>
		<title>Copying an object in C# and Visual Basic</title>
		<link>http://lukhezo.com/2011/12/05/copying-an-object-in-c-and-visual-basic/</link>
		<comments>http://lukhezo.com/2011/12/05/copying-an-object-in-c-and-visual-basic/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 18:31:19 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[copying object]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/?p=1157</guid>
		<description><![CDATA[Sometimes there are classes you find you use in every project. I remember once (shudder) I, rather inelegantly instantiated a class, and performed a manual copy of all the properties in the object (when the pressure was on, and I just needed to make the thing work). I have also found myself spending hours chasing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1157&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes there are classes you find you use in every project. I remember once (shudder) I, rather inelegantly instantiated a class, and performed a manual copy of all the properties in the object (when the pressure was on, and I just needed to make the thing work).</p>
<p>I have also found myself spending hours chasing subtle and hard to debug issues where <a href="http://msdn.microsoft.com/en-us/library/system.icloneable(v=VS.100).aspx">ICloneable</a> has been used -&#160; which, incidentally, is widely accepted as an interface one ought never use – or <a href="http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx">Object.MemberwiseClone</a>. It turns out the only way to do this correctly, is to serialise the object, and then use the deserialised object, implemented here using an extension method.</p>
<p>Even though this is quite straightforward, I still am posting this however , because I find I need to use this functionality frequently, and often I no longer have access to the source, or it takes ages to find the class. The Visual Basic code sample is after the C# one.</p>
<h3>C#</h3>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div><span style="color:#0000ff;">using</span> System.IO;</div>
<div><span style="color:#0000ff;">using</span> System.Runtime.Serialization;</div>
<div><span style="color:#0000ff;">using</span> System.Runtime.Serialization.Formatters.Binary;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ObjectCopierApp.Extensions</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Provides a method for performing deep copying of an object. </span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Binary Serialization is used to perform the copy. </span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">ObjectCopier</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Perform a deep copy of an object. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;typeparam name=&quot;T&quot;&gt;</span><span style="color:#008000;">The type of object being copied.</span><span style="color:#808080;">&lt;/typeparam&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;source&quot;&gt;</span><span style="color:#008000;">The object instance to copy.</span><span style="color:#808080;">&lt;/param&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">The copied object.</span><span style="color:#808080;">&lt;/returns&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> T Clone&lt;T&gt;(<span style="color:#0000ff;">this</span> T source)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">typeof</span>(T).IsSerializable)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;The type must be serializable.&quot;</span>, <span style="color:#a31515;">&quot;source&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Don&#8217;t serialize a null object, simply return the default for that object </span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (ReferenceEquals(source, <span style="color:#0000ff;">null</span>))</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">return</span> <span style="color:#0000ff;">default</span>(T);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">IFormatter</span> formatter = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">BinaryFormatter</span>();</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> stream = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">MemoryStream</span>())</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">formatter.Serialize(stream, source);</div>
<div style="text-indent:-4ex;margin-left:20ex;">stream.Seek(0, <span style="color:#2b91af;">SeekOrigin</span>.Begin);</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">return</span> (T)formatter.Deserialize(stream);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">} </div>
<div>}</div>
<div>&#160;</div>
<div>&#160;</div>
</p></div>
<h3>Visual Basic</h3>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">Imports</span> System.IO</div>
<div><span style="color:#0000ff;">Imports</span> System.Runtime.Serialization</div>
<div><span style="color:#0000ff;">Imports</span> System.Runtime.Serialization.Formatters.Binary</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">Namespace</span> ObjectCopierApp.Extensions</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#008000;">&#8221;&#8217; Provides a method for performing deep copying of an object. </span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#008000;">&#8221;&#8217; Binary Serialization is used to perform the copy. </span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;/summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">Friend</span> <span style="color:#0000ff;">NotInheritable</span> <span style="color:#0000ff;">Class</span> <span style="color:#2b91af;">ObjectCopier</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">Private</span> <span style="color:#0000ff;">Sub</span> <span style="color:#0000ff;">New</span>()</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; Perform a deep copy of an object. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;/summary&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;typeparam name=&quot;T&quot;&gt;</span><span style="color:#008000;">The type of object being copied.</span><span style="color:#808080;">&lt;/typeparam&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;param name=&quot;source&quot;&gt;</span><span style="color:#008000;">The object instance to copy.</span><span style="color:#808080;">&lt;/param&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">&#8221;&#8217; </span><span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">The copied object.</span><span style="color:#808080;">&lt;/returns&gt;</span> </div>
<div style="text-indent:-4ex;margin-left:12ex;">&lt;System.Runtime.CompilerServices.<span style="color:#2b91af;">Extension</span>()&gt; _</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">Friend</span> <span style="color:#0000ff;">Shared</span> <span style="color:#0000ff;">Function</span> Clone(<span style="color:#0000ff;">Of</span> T)(source <span style="color:#0000ff;">As</span> <span style="color:#2b91af;">T</span>) <span style="color:#0000ff;">As</span> <span style="color:#2b91af;">T</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">If</span> <span style="color:#0000ff;">Not</span> <span style="color:#0000ff;">GetType</span>(<span style="color:#2b91af;">T</span>).IsSerializable <span style="color:#0000ff;">Then</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">Throw</span> <span style="color:#0000ff;">New</span> <span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;The type must be serializable.&quot;</span>, <span style="color:#a31515;">&quot;source&quot;</span>)</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">If</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">&#8216; Don&#8217;t serialize a null object, simply return the default for that object </span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">If</span> ReferenceEquals(source, <span style="color:#0000ff;">Nothing</span>) <span style="color:#0000ff;">Then</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">Return</span> <span style="color:#0000ff;">Nothing</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">If</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">Dim</span> formatter <span style="color:#0000ff;">As</span> <span style="color:#2b91af;">IFormatter</span> = <span style="color:#0000ff;">New</span> <span style="color:#2b91af;">BinaryFormatter</span>()</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">Using</span> stream = <span style="color:#0000ff;">New</span> <span style="color:#2b91af;">MemoryStream</span>()</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">formatter</span>.Serialize(stream, source)</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">stream</span>.Seek(0, <span style="color:#2b91af;">SeekOrigin</span>.Begin)</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">Return</span> <span style="color:#0000ff;">DirectCast</span>(<span style="color:#2b91af;">formatter</span>.Deserialize(stream), <span style="color:#2b91af;">T</span>)</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Using</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Function</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Class</span></div>
<div><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Namespace</span></div>
</p></div>
<br />Filed under: <a href='http://lukhezo.com/category/silverlight/'>Silverlight</a>, <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a>, <a href='http://lukhezo.com/category/windows-phone-7/'>Windows Phone 7</a>, <a href='http://lukhezo.com/category/wpf/'>WPF</a> Tagged: <a href='http://lukhezo.com/tag/copying-object/'>copying object</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1157/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1157&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/12/05/copying-an-object-in-c-and-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>
	</item>
		<item>
		<title>Cannot build Expression Blend Project using F5/Ctrl+Shift+B</title>
		<link>http://lukhezo.com/2011/11/20/cannot-build-expression-blend-project-using-f5ctrlshiftb/</link>
		<comments>http://lukhezo.com/2011/11/20/cannot-build-expression-blend-project-using-f5ctrlshiftb/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 13:06:29 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/?p=1154</guid>
		<description><![CDATA[Whenever I try build Expression Blend on a machine for the first time, I always encounter this error when I hit F5 The specified solution configuration &#34;Debug&#124;HPD&#34; is invalid. Please specify a valid solution configuration using the Configuration and Platform properties (e.g. MSBuild.exe Solution.sln /p:Configuration=Debug /p:Platform=&#34;Any CPU&#34;) or leave those properties blank to use the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1154&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Whenever I try build Expression Blend on a machine for the first time, I always encounter this error when I hit F5 </p>
<p><em> The specified solution configuration &quot;Debug|HPD&quot; is invalid. Please specify a valid solution configuration using the Configuration and Platform properties (e.g. MSBuild.exe Solution.sln /p:Configuration=Debug /p:Platform=&quot;Any CPU&quot;) or leave those properties blank to use the default solution configuration.     <br />Done building project &quot;<strong>Name Of Your Project.sln</strong>&quot; &#8212; FAILED.</em></p>
<p><a href="http://castalian.files.wordpress.com/2011/11/error.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="Error" border="0" alt="Error" src="http://castalian.files.wordpress.com/2011/11/error_thumb.png?w=644&#038;h=385" width="644" height="385" /></a></p>
<p>It goes without saying that it is irritating that you cannot use the application from the word go. I have encountered this error on both 32 and 64 bit windows versions, and resolve the issue thus;</p>
<p>Go into the advanced system settings (I am using windows 7)</p>
<p><a href="http://castalian.files.wordpress.com/2011/11/01.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="01" border="0" alt="01" src="http://castalian.files.wordpress.com/2011/11/01_thumb.png?w=644&#038;h=341" width="644" height="341" /></a></p>
<p>Select the advanced tab, and click the “Environment Variables” button</p>
<p><a href="http://castalian.files.wordpress.com/2011/11/02.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="02" border="0" alt="02" src="http://castalian.files.wordpress.com/2011/11/02_thumb.png?w=435&#038;h=484" width="435" height="484" /></a></p>
<p>Delete the “Platform” variable</p>
<p><a href="http://castalian.files.wordpress.com/2011/11/03.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="03" border="0" alt="03" src="http://castalian.files.wordpress.com/2011/11/03_thumb.png?w=438&#038;h=484" width="438" height="484" /></a></p>
<p>After restarting Expression blend, you should find that you can F5/Ctrl+Shift+B to build your solution.</p>
<br />Filed under: <a href='http://lukhezo.com/category/general/'>General</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1154&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/11/20/cannot-build-expression-blend-project-using-f5ctrlshiftb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/11/error_thumb.png" medium="image">
			<media:title type="html">Error</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/11/01_thumb.png" medium="image">
			<media:title type="html">01</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/11/02_thumb.png" medium="image">
			<media:title type="html">02</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/11/03_thumb.png" medium="image">
			<media:title type="html">03</media:title>
		</media:content>
	</item>
		<item>
		<title>Encrypting files in C#.NET using the Advanced Encryption Standard (AES)</title>
		<link>http://lukhezo.com/2011/11/06/encrypting-files-in-net-using-the-advanced-encryption-standard-aes/</link>
		<comments>http://lukhezo.com/2011/11/06/encrypting-files-in-net-using-the-advanced-encryption-standard-aes/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 13:00:39 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/2011/11/06/encrypting-files-in-net-using-the-advanced-encryption-standard-aes/</guid>
		<description><![CDATA[Cryptography One of the biggest challenges when dealing with the security and encryption for a system, is the determination of the correct ciphering paradigm. In .NET, there is a copious amount of libraries available for use in the System.Cryptography namespace. A significant amount of these libraries have been deprecated, usually, due to vulnerabilities being subsequently [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1136&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Cryptography </h3>
<p>One of the biggest challenges when dealing with the security and encryption for a system, is the determination of the correct ciphering paradigm. In .NET, there is a copious amount of libraries available for use in the <b>System.Cryptography</b> namespace. A significant amount of these libraries have been deprecated, usually, due to vulnerabilities being subsequently exposed, so it is very easy to use something that may be as watertight as a sieve.</p>
<p>This is further compounded by the fact that the cryptography API’s are very detailed and low level – they are not easy to use for a novice – the consequences of setting a single parameter incorrectly results in a security implementation that may as well not exist. Consequently, it is imperative that this subject never be approached in a typical agile/sprint manner – security should definitely be approached using a <i>waterfall</i> model. Have no hesitation to advise any manager or architect that your solution “will be ready, when it is ready”. The agile methodology is typically about adding units of functionality in a <a href="http://en.wikipedia.org/wiki/You_ain't_gonna_need_it">YAGNI</a> way, accruing technical debt that can be paid back later, and refactoring applied, this just simply not a correct or acceptable approach when dealing with the security of a system. Do ensure you take the time to do a lot of research, understanding the pitfalls of various implementations is vital to a robust security implementation.</p>
<h3>The Advanced Encryption Standard (AES)</h3>
<p>The abundance of so many different types of cryptography, implemented using Symmetric (same key is used to encrypt and decrypt) and Asymmetric (public key and private key used to encrypt and decrypt) algorithms has necessitated that Governments try and standardise implementations across departments, sites and even countries. The AES was <a href="http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf">released</a> in 2001 as a replacement for the Data Encryption Standard (DES) which had been found to be susceptible to backdoors. This new standard has been widely adopted in commercial environments, as it had a requirement to be able to protect information for a minimum of 20 years or 30 years. </p>
<p>A number of papers were submitted in the application process for the AES by various academic institutions, with the winning cipher named <strong>Rijndael</strong> (pronounced rain-dahl) a play on the names of the authors of the paper, Joan Daemen and Vincent Rijmen (paper available <a href="http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf">here</a>). I am sure you will agree that comprehension and implementation of the paper is better suited to domain experts. The algorithm was written by two gifted PhD calibre researchers, so your time as a developer is better suited to try and resolve the domain problems that your business is trying to solve (unless you are a cryptographer of course). You can be sure that researchers at Microsoft have done all the time consuming work of implementing and testing the algorithm, rather than to trying to implement the Rijndael Block Cipher yourself.</p>
<p>To this end, Microsoft have implemented the Rijndael Block Cipher in two in .NET&#160; classes which, incidentally, both inherit from the <strong>SymmetricAlgorithm</strong>&#160; abstract base class</p>
<ul>
<li>RijndaelManaged. </li>
<li>AesManaged </li>
</ul>
<ul>The AES algorithm is essentially the Rijndael symmetric algorithm with a fixed block size and iteration count. This class functions the same way as the <strong>RijndaelManaged</strong> class but limits blocks to 128 bits and does not allow feedback modes. Most developers tend to favour using the <strong>RijndaelManaged</strong> class directly, as that is the one that is used in the <a href="http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf">FIPS-197 specification for AES</a> but there are a couple of caveats. If you want to use <strong>RijndaelManaged</strong>&#160; as AES and adhere to the specification ensure </ul>
<ol>
<li>You set the block size to 128 bits&#160; </li>
<li>You do not use CFB mode, if you do, ensure the feedback size is also 128 bits&#160; </li>
</ol>
<p>Unlike some of the asymmetric implementations by Microsoft, the AES implementation allows you to work at a very high level of abstraction, reducing the amount of parameters you have to configure, hence the scope for error. I have created a class that allows you to encrypt and decrypt strings (your password), and then use this to encrypt a files from anywhere on your machine. </p>
<p>Thus far, the only way this algorithm can be broken is by using a technique known as <em>brute force</em>. This is done by a supercomputer(s) trying every known word in a language, and various password to try and generate the correct password. Typically, these types of programs run over weeks or even months, but can be increased&#160; to millennia if the end user chooses a <em>strong</em> password to begin with, which is why having a well defined password policy is vital.</p>
<div style="font-family:monospace;">
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> MainWindow()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;">InitializeComponent();</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">byte</span>[] encryptedPassword;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Create a new instance of the RijndaelManaged</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// class.&#160; This generates a new key and initialization </span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// vector (IV).</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> algorithm = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RijndaelManaged</span>())</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">algorithm.KeySize = 256;</div>
<div style="text-indent:-4ex;margin-left:20ex;">algorithm.BlockSize = 128;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#008000;">// Encrypt the string to an array of bytes.</span></div>
<div style="text-indent:-4ex;margin-left:20ex;">encryptedPassword = <span style="color:#2b91af;">Cryptology</span>.EncryptStringToBytes(<span style="color:#a31515;">&quot;Password&quot;</span>, algorithm.Key, algorithm.IV);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">string</span> chars = encryptedPassword.Aggregate(<span style="color:#0000ff;">string</span>.Empty, (current, b) =&gt; current + b.ToString());</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Cryptology</span>.EncryptFile(<span style="color:#a31515;">@&quot;C:\Users\Ira\Downloads\test.txt&quot;</span>, <span style="color:#a31515;">@&quot;C:\Users\Ira\Downloads\encrypted_test.txt&quot;</span>, chars);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Cryptology</span>.DecryptFile(<span style="color:#a31515;">@&quot;C:\Users\Ira\Downloads\encrypted_test.txt&quot;</span>, <span style="color:#a31515;">@&quot;C:\Users\Ira\Downloads\unencyrpted_test.txt&quot;</span>, chars);</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
</p></div>
<p>I am using 256 bit (you can change this to 128 or 192)</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div><span style="color:#0000ff;">using</span> System.IO;</div>
<div><span style="color:#0000ff;">using</span> System.Security.Cryptography;</div>
<div><span style="color:#0000ff;">using</span> System.Text;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> AesApp.Rijndael</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">sealed</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Cryptology</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">string</span> Salt = <span style="color:#a31515;">&quot;d5fg4df5sg4ds5fg45sdfg4&quot;</span>;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> SizeOfBuffer = 1024*8;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span>[] EncryptStringToBytes(<span style="color:#0000ff;">string</span> plainText, <span style="color:#0000ff;">byte</span>[] key, <span style="color:#0000ff;">byte</span>[] iv)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Check arguments.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (plainText == <span style="color:#0000ff;">null</span> || plainText.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;plainText&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (key == <span style="color:#0000ff;">null</span> || key.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;key&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (iv == <span style="color:#0000ff;">null</span> || iv.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;key&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">byte</span>[] encrypted;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Create an RijndaelManaged object</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// with the specified key and IV.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> rijAlg = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RijndaelManaged</span>())</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">rijAlg.Key = key;</div>
<div style="text-indent:-4ex;margin-left:20ex;">rijAlg.IV = iv;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#008000;">// Create a decrytor to perform the stream transform.</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">ICryptoTransform</span> encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#008000;">// Create the streams used for encryption.</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> msEncrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">MemoryStream</span>())</div>
<div style="text-indent:-4ex;margin-left:20ex;">{</div>
<div style="text-indent:-4ex;margin-left:24ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> csEncrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">CryptoStream</span>(msEncrypt, encryptor, <span style="color:#2b91af;">CryptoStreamMode</span>.Write))</div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> swEncrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StreamWriter</span>(csEncrypt))</div>
<div style="text-indent:-4ex;margin-left:28ex;">{</div>
<div style="text-indent:-4ex;margin-left:32ex;"><span style="color:#008000;">//Write all data to the stream.</span></div>
<div style="text-indent:-4ex;margin-left:32ex;">swEncrypt.Write(plainText);</div>
<div style="text-indent:-4ex;margin-left:28ex;">}</div>
<div style="text-indent:-4ex;margin-left:28ex;">encrypted = msEncrypt.ToArray();</div>
<div style="text-indent:-4ex;margin-left:24ex;">}</div>
<div style="text-indent:-4ex;margin-left:20ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Return the encrypted bytes from the memory stream.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> encrypted;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> DecryptStringFromBytes(<span style="color:#0000ff;">byte</span>[] cipherText, <span style="color:#0000ff;">byte</span>[] key, <span style="color:#0000ff;">byte</span>[] iv)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Check arguments.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (cipherText == <span style="color:#0000ff;">null</span> || cipherText.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;cipherText&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (key == <span style="color:#0000ff;">null</span> || key.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;key&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (iv == <span style="color:#0000ff;">null</span> || iv.Length &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&quot;key&quot;</span>);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Declare the string used to hold</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// the decrypted text.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">string</span> plaintext;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Create an RijndaelManaged object</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// with the specified key and IV.</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> rijAlg = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RijndaelManaged</span>())</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">rijAlg.Key = key;</div>
<div style="text-indent:-4ex;margin-left:20ex;">rijAlg.IV = iv;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#008000;">// Create a decrytor to perform the stream transform.</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">ICryptoTransform</span> decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#008000;">// Create the streams used for decryption.</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> msDecrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">MemoryStream</span>(cipherText))</div>
<div style="text-indent:-4ex;margin-left:20ex;">{</div>
<div style="text-indent:-4ex;margin-left:24ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> csDecrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">CryptoStream</span>(msDecrypt, decryptor, <span style="color:#2b91af;">CryptoStreamMode</span>.Read))</div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> srDecrypt = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StreamReader</span>(csDecrypt))</div>
<div style="text-indent:-4ex;margin-left:28ex;">{</div>
<div style="text-indent:-4ex;margin-left:32ex;"><span style="color:#008000;">// Read the decrypted bytes from the decrypting stream</span></div>
<div style="text-indent:-4ex;margin-left:32ex;"><span style="color:#008000;">// and place them in a string.</span></div>
<div style="text-indent:-4ex;margin-left:32ex;">plaintext = srDecrypt.ReadToEnd();</div>
<div style="text-indent:-4ex;margin-left:28ex;">}</div>
<div style="text-indent:-4ex;margin-left:24ex;">}</div>
<div style="text-indent:-4ex;margin-left:20ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> plaintext;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> EncryptFile(<span style="color:#0000ff;">string</span> inputPath, <span style="color:#0000ff;">string</span> outputPath, <span style="color:#0000ff;">string</span> password)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> input = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FileStream</span>(inputPath, <span style="color:#2b91af;">FileMode</span>.Open, <span style="color:#2b91af;">FileAccess</span>.Read);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> output = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FileStream</span>(outputPath, <span style="color:#2b91af;">FileMode</span>.OpenOrCreate, <span style="color:#2b91af;">FileAccess</span>.Write);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// 1.The block size is set to 128 bits</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> algorithm = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RijndaelManaged</span> {KeySize = 256, BlockSize = 128};</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> key = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Rfc2898DeriveBytes</span>(password, <span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(Salt));</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;">algorithm.Key = key.GetBytes(algorithm.KeySize/8);</div>
<div style="text-indent:-4ex;margin-left:16ex;">algorithm.IV = key.GetBytes(algorithm.BlockSize/8);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> encryptedStream = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">CryptoStream</span>(output, algorithm.CreateEncryptor(), <span style="color:#2b91af;">CryptoStreamMode</span>.Write))</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">CopyStream(input, encryptedStream);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> DecryptFile(<span style="color:#0000ff;">string</span> inputPath, <span style="color:#0000ff;">string</span> outputPath, <span style="color:#0000ff;">string</span> password)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> input = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FileStream</span>(inputPath, <span style="color:#2b91af;">FileMode</span>.Open, <span style="color:#2b91af;">FileAccess</span>.Read);</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> output = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FileStream</span>(outputPath, <span style="color:#2b91af;">FileMode</span>.OpenOrCreate, <span style="color:#2b91af;">FileAccess</span>.Write);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// 1.The block size is set to 128 bits</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> algorithm = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RijndaelManaged</span> {KeySize = 256, BlockSize = 128};</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> key = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Rfc2898DeriveBytes</span>(password, <span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(Salt));</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;">algorithm.Key = key.GetBytes(algorithm.KeySize/8);</div>
<div style="text-indent:-4ex;margin-left:16ex;">algorithm.IV = key.GetBytes(algorithm.BlockSize/8);</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">try</span></div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> decryptedStream = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">CryptoStream</span>(output, algorithm.CreateDecryptor(), <span style="color:#2b91af;">CryptoStreamMode</span>.Write))</div>
<div style="text-indent:-4ex;margin-left:20ex;">{</div>
<div style="text-indent:-4ex;margin-left:24ex;">CopyStream(input, decryptedStream);</div>
<div style="text-indent:-4ex;margin-left:20ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">CryptographicException</span>)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">InvalidDataException</span>(<span style="color:#a31515;">&quot;Please supply a correct password&quot;</span>);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Exception</span>(ex.Message);</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> CopyStream(<span style="color:#2b91af;">Stream</span> input, <span style="color:#2b91af;">Stream</span> output)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (output)</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">using</span> (input)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">byte</span>[] buffer = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[SizeOfBuffer];</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">int</span> read;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">while</span> ((read = input.Read(buffer, 0, buffer.Length)) &gt; 0)</div>
<div style="text-indent:-4ex;margin-left:20ex;">{</div>
<div style="text-indent:-4ex;margin-left:24ex;">output.Write(buffer, 0, read);</div>
<div style="text-indent:-4ex;margin-left:20ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div></div>
</p></div>
<p> You can download the <strong>AesApp</strong> <a href="https://skydrive.live.com/redir.aspx?cid=f9b0e018a02bb3d4&amp;resid=F9B0E018A02BB3D4!126">here</a>.</p>
<br />Filed under: <a href='http://lukhezo.com/category/general/'>General</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1136&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/11/06/encrypting-files-in-net-using-the-advanced-encryption-standard-aes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>
	</item>
		<item>
		<title>ItemsControl Performance Improvements in .NET 4.5</title>
		<link>http://lukhezo.com/2011/09/24/itemscontrol-performance-improvements-in-net-4-5/</link>
		<comments>http://lukhezo.com/2011/09/24/itemscontrol-performance-improvements-in-net-4-5/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 09:32:20 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Faster]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Sluggish]]></category>
		<category><![CDATA[Speed]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/2011/09/24/itemscontrol-performance-improvements-in-net-4-5/</guid>
		<description><![CDATA[WPF is Slow One of the most common complaints from C++ or Windows Forms developers moving to WPF/XAML is the issue of performance. I have worked exclusively and intricately with WPF/XAML for the last 4 years and at times have come across performance issues that put me in a corner, including; I needed to enumerate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1127&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>WPF is Slow</h3>
<p>One of the most common complaints from C++ or Windows Forms developers moving to WPF/XAML is the issue of performance. I have worked exclusively and intricately with WPF/XAML for the last 4 years and at times have come across performance issues that put me in a corner, including;</p>
<ul>
<li>I needed to enumerate all the alarm and safety devices in an Airports security system inventory. This included fire, smoke, travelator, escalator, motion detection etc. in effect I needed to load up thousands of hardware devices into the WPF application for configuration </li>
<li>I was dealing with scientific data for DNA, RNA and Protein. Typically you would have an image with an <a href="http://en.wikipedia.org/wiki/Electropherogram">electropherogram</a> attached to it, which contains thousands of points of floating point data. Scientists required the ability to step through several different samples quickly in order to perform analysis on this data. </li>
</ul>
<ul>In both these applications, I ended up having to make significant compromises, including using a Windows Forms chart in the scientific application as loading thousands of floating point data into the plethora of commercial and open source WPF charts we tried was unbearably slow. The Windows Forms <a href="http://www.codeproject.com/KB/graphics/zedgraph.aspx">chart</a> would render the data in milliseconds, where WPF would take a minimum of 10 or 20 seconds with the same data-set, and given the fact that we needed to extend the chart with additional functionality, it added several months worth of development to alter the Windows Forms chart, where it would have been far easier and less time consuming using WPF.</ul>
<ul>
<h3>Staggering</h3>
<p> It turns out that peoples cries about the sluggish nature of WPF applications are correct. Microsoft have managed to attain simply staggering performance improvements to the <strong>ItemsControl</strong> in WPF. An <strong>ItemsControl</strong> represents a control that can be used to present a collection of items. In WPF, this includes the <em>TreeView</em>, <em>ListBox</em>, <em>ListView</em> and <em>DataGrid</em> controls that are built using <strong>ItemsControls</strong>.</ul>
<h1><a href="http://castalian.files.wordpress.com/2011/09/dotnet4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="DotNet4" border="0" alt="DotNet4" src="http://castalian.files.wordpress.com/2011/09/dotnet4_thumb.png?w=644&#038;h=306" width="644" height="306" /></a></h1>
<p>In .NET 4 (Visual Studio 2010) and previous, a test was conducted where 12 000 items are loaded into an <strong>ItemsControl</strong>, the results are in the image below</p>
<p><a href="http://castalian.files.wordpress.com/2011/09/outofmemory.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="OutOfMemory" border="0" alt="OutOfMemory" src="http://castalian.files.wordpress.com/2011/09/outofmemory_thumb.png?w=644&#038;h=303" width="644" height="303" /></a></p>
<p>After about 7 minutes, the computer throws an out of memory exception, which I am sure you will agree is absolutely terrible. Microsoft prioritised this issue and in the first iteration managed to increase the total number of items loaded to 200 000 (from 12 000) and load these in 24.5 seconds, an incredible improvement.</p>
<p><a href="http://castalian.files.wordpress.com/2011/09/firstiteration.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="FirstIteration" border="0" alt="FirstIteration" src="http://castalian.files.wordpress.com/2011/09/firstiteration_thumb.png?w=644&#038;h=305" width="644" height="305" /></a></p>
<p>The <em>Pièce de résistance</em> is that they continued to try and get this already considerable metric down even further, in the end&#160; they got this down to <strong>2.3 seconds</strong>. </p>
<p><a href="http://castalian.files.wordpress.com/2011/09/final.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="Final" border="0" alt="Final" src="http://castalian.files.wordpress.com/2011/09/final_thumb.png?w=644&#038;h=271" width="644" height="271" /></a></p>
<p>If you have a WPF application that is data centric, especially handling thousands of rows of data, then upgrading to .NET 4.5 is an absolute no brainer. WPF is now at least 182 times quicker whilst handling 16 times <em>more</em> data.</p>
<p>It means that WPF is going to be significantly faster with the additional improvements that have been made to virtualisation in <strong>ItemsControls</strong> and Cold/Warm start-up times for .NET applications in general.</p>
<br />Filed under: <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a>, <a href='http://lukhezo.com/category/wpf/'>WPF</a> Tagged: <a href='http://lukhezo.com/tag/faster/'>Faster</a>, <a href='http://lukhezo.com/tag/performance/'>Performance</a>, <a href='http://lukhezo.com/tag/silverlight/'>Silverlight</a>, <a href='http://lukhezo.com/tag/sluggish/'>Sluggish</a>, <a href='http://lukhezo.com/tag/speed/'>Speed</a>, <a href='http://lukhezo.com/tag/wpf/'>WPF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1127&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/09/24/itemscontrol-performance-improvements-in-net-4-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/09/dotnet4_thumb.png" medium="image">
			<media:title type="html">DotNet4</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/09/outofmemory_thumb.png" medium="image">
			<media:title type="html">OutOfMemory</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/09/firstiteration_thumb.png" medium="image">
			<media:title type="html">FirstIteration</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/09/final_thumb.png" medium="image">
			<media:title type="html">Final</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculate range/limit with C#</title>
		<link>http://lukhezo.com/2011/06/02/calculate-rangelimit-with-c/</link>
		<comments>http://lukhezo.com/2011/06/02/calculate-rangelimit-with-c/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 20:56:47 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Calculate]]></category>
		<category><![CDATA[Determine]]></category>
		<category><![CDATA[Limit]]></category>
		<category><![CDATA[Range]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/2011/06/02/calculate-rangelimit-with-c/</guid>
		<description><![CDATA[A recurring requirement in every day programming is determining whether a value exists with a certain range. Since it is exam time (for most students at present), I resolved to write a quick application demonstrating this requirement based around examination marks. This quick demo is written in WPF, but should work in Silverlight or Windows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1102&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A recurring requirement in every day programming is determining whether a value exists with a certain range. Since it is exam time (for most students at present), I resolved to write a quick application demonstrating this requirement based around examination marks. </p>
<p>This quick demo is written in WPF, but should work in Silverlight or Windows Phone 7 with no alteration. The <strong>Range</strong> class though, can be used throughout .NET in general.</p>
<p>To start with, create a new WPF application in visual Studio and call it <strong>ExamApp</strong>. Add a new folder to the project called <strong>Range</strong> and in this declare a new <em>interface</em> called <strong>IRange</strong> with the following members</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp.Range</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> <span style="color:#2b91af;">IRange</span>&lt;T&gt; <span style="color:#0000ff;">where</span> T : <span style="color:#2b91af;">IComparable</span>&lt;T&gt;</div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;">T Start { <span style="color:#0000ff;">get</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;">T End { <span style="color:#0000ff;">get</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">bool</span> InRange(T valueToFind);</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
</p></div>
<p>Create a new class in the same folder called <strong>Range</strong> which implements the <em>interface</em> above</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp.Range</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Range</span>&lt;T&gt; : <span style="color:#2b91af;">IRange</span>&lt;T&gt; <span style="color:#0000ff;">where</span> T : <span style="color:#2b91af;">IComparable</span>&lt;T&gt;</div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> T start;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> T end;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> Range(T start, T end)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (start.CompareTo(end) &lt;= 0)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">this</span>.start = start;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">this</span>.end = end;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">else</span></div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">this</span>.start = end;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">this</span>.end = start;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> T Start</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">get</span> { <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>.start; }</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> T End</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">get</span> { <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>.end; }</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> InRange(T valueToFind)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> valueToFind.CompareTo(Start) &gt;= 0 &amp;&amp; valueToFind.CompareTo(End) &lt;= 0;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>Add a new Folder to the project called <strong>Examinations</strong> and in this add a new class called <strong>Percentage</strong>. This <strong>Percentage</strong> class inherits off <strong>Range</strong>, and here you can create whatever logical range class you may desire for your own applications</p>
<p><span style="color:#0000ff;">using</span> System;</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> ExamApp.Range;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp.Examinations</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Percentage</span>&lt;T&gt; : <span style="color:#2b91af;">Range</span>&lt;T&gt; <span style="color:#0000ff;">where</span> T : <span style="color:#2b91af;">IComparable</span>&lt;T&gt;</div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> Percentage(T start, T end) : <span style="color:#0000ff;">base</span>(start, end)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>Add a new <em>enumeration</em> called <strong>Grade</strong> thus</p>
<p><span style="color:#0000ff;">namespace</span> ExamApp.Examinations</p>
<div style="font-family:monospace;">
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> <span style="color:#2b91af;">Grade</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;">None = 0,</div>
<div style="text-indent:-4ex;margin-left:12ex;">A = 1,</div>
<div style="text-indent:-4ex;margin-left:12ex;">B = 2,</div>
<div style="text-indent:-4ex;margin-left:12ex;">C = 3,</div>
<div style="text-indent:-4ex;margin-left:12ex;">D = 4,</div>
<div style="text-indent:-4ex;margin-left:12ex;">E = 5,</div>
<div style="text-indent:-4ex;margin-left:8ex;">} ;</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>Add a new <em>interface</em> called <strong>ITest</strong> with the following members.</p>
<p><span style="color:#0000ff;">namespace</span> ExamApp.Examinations</p>
<div style="font-family:monospace;">
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">interface</span> <span style="color:#2b91af;">ITest</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">int</span> ArtAndDesignMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">int</span> ScienceMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">int</span> MathsMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>Add a new folder called <strong>Converters</strong> to the project, and add the following</p>
<p><span style="color:#0000ff;">using</span> System;</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System.Collections.Generic;</div>
<div><span style="color:#0000ff;">using</span> System.Globalization;</div>
<div><span style="color:#0000ff;">using</span> System.Linq;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Data;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Markup;</div>
<div><span style="color:#0000ff;">using</span> ExamApp.Examinations;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp.Converters</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">MarkToGradeConverter</span> : <span style="color:#2b91af;">MarkupExtension</span>, <span style="color:#2b91af;">IValueConverter</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">MarkToGradeConverter</span> instance;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;, <span style="color:#2b91af;">Grade</span>&gt; Marks;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">static</span> MarkToGradeConverter()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;">Marks = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;, <span style="color:#2b91af;">Grade</span>&gt;</div>
<div style="text-indent:-4ex;margin-left:28ex;">{</div>
<div style="text-indent:-4ex;margin-left:32ex;">{<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;(80, 100), <span style="color:#2b91af;">Grade</span>.A},</div>
<div style="text-indent:-4ex;margin-left:32ex;">{<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;(75, 79), <span style="color:#2b91af;">Grade</span>.B},</div>
<div style="text-indent:-4ex;margin-left:32ex;">{<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;(60, 74), <span style="color:#2b91af;">Grade</span>.C},</div>
<div style="text-indent:-4ex;margin-left:32ex;">{<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;(55, 59), <span style="color:#2b91af;">Grade</span>.D},</div>
<div style="text-indent:-4ex;margin-left:32ex;">{<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt;(0, 54), <span style="color:#2b91af;">Grade</span>.E},</div>
<div style="text-indent:-4ex;margin-left:28ex;">};</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">object</span> ProvideValue(<span style="color:#2b91af;">IServiceProvider</span> serviceProvider)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> instance ?? (instance = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">MarkToGradeConverter</span>());</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#region</span> Implementation of IValueConverter</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> Convert(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> GetGrade((<span style="color:#0000ff;">int</span>)value).ToString();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ConvertBack(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#00008b;">NotImplementedException</span>();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">Grade</span> GetGrade(<span style="color:#0000ff;">int</span> mark)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#2b91af;">Percentage</span>&lt;<span style="color:#0000ff;">int</span>&gt; key = Marks.Keys.FirstOrDefault(r =&gt; r.InRange(mark));</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (key != <span style="color:#0000ff;">null</span>)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">Grade</span> grade;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">if</span> (Marks.TryGetValue(key, <span style="color:#0000ff;">out</span> grade))</div>
<div style="text-indent:-4ex;margin-left:20ex;">{</div>
<div style="text-indent:-4ex;margin-left:24ex;"><span style="color:#0000ff;">return</span> grade;</div>
<div style="text-indent:-4ex;margin-left:20ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> <span style="color:#2b91af;">Grade</span>.None;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#endregion</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>As you can see most of the logic is executed here. Add a new class to the project called <strong>Student</strong> with the following properties. Also note that this implements the <strong>ITest</strong> <em>interface</em> that was declared earlier</p>
<p><span style="color:#0000ff;">using</span> ExamApp.Examinations;</p>
<div style="font-family:monospace;">
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Student</span> : <span style="color:#2b91af;">ITest</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> Student(<span style="color:#0000ff;">string</span> firstName, <span style="color:#0000ff;">string</span> surname, <span style="color:#0000ff;">int</span> artAndDesignMark, <span style="color:#0000ff;">int</span> scienceMark, <span style="color:#0000ff;">int</span> mathsMark)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.FirstName = firstName;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.Surname = surname;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.ArtAndDesignMark = artAndDesignMark;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.ScienceMark = scienceMark;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.MathsMark = mathsMark;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> Student()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> FirstName { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Surname { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#region</span> Implementation of ITest</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> ArtAndDesignMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> ScienceMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> MathsMark { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#endregion</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>We need a collection of students to show in a list so create the following class that inherits off <strong>ObservableCollection&lt;T&gt;</strong></p>
<p><span style="color:#0000ff;">using</span> System.Collections.ObjectModel;</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">namespace</span> ExamApp</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Students</span> : <span style="color:#2b91af;">ObservableCollection</span>&lt;<span style="color:#2b91af;">Student</span>&gt;</div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> Students()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;">Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Student</span></div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;">FirstName = <span style="color:#a31515;">&quot;John&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">Surname = <span style="color:#a31515;">&quot;Lennon&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ArtAndDesignMark = 81,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ScienceMark = 59,</div>
<div style="text-indent:-4ex;margin-left:28ex;">MathsMark = 77</div>
<div style="text-indent:-4ex;margin-left:24ex;">});</div>
<div style="text-indent:-4ex;margin-left:16ex;">Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Student</span></div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;">FirstName = <span style="color:#a31515;">&quot;Paul&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">Surname = <span style="color:#a31515;">&quot;McCartney&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ArtAndDesignMark = 88,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ScienceMark = 40,</div>
<div style="text-indent:-4ex;margin-left:28ex;">MathsMark = 66</div>
<div style="text-indent:-4ex;margin-left:24ex;">});</div>
<div style="text-indent:-4ex;margin-left:16ex;">Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Student</span></div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;">FirstName = <span style="color:#a31515;">&quot;Ringo&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">Surname = <span style="color:#a31515;">&quot;Starr&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ArtAndDesignMark = 88,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ScienceMark = 31,</div>
<div style="text-indent:-4ex;margin-left:28ex;">MathsMark = 96</div>
<div style="text-indent:-4ex;margin-left:24ex;">});</div>
<div style="text-indent:-4ex;margin-left:16ex;">Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Student</span></div>
<div style="text-indent:-4ex;margin-left:24ex;">{</div>
<div style="text-indent:-4ex;margin-left:28ex;">FirstName = <span style="color:#a31515;">&quot;George&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">Surname = <span style="color:#a31515;">&quot;Harrison&quot;</span>,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ArtAndDesignMark = 100,</div>
<div style="text-indent:-4ex;margin-left:28ex;">ScienceMark = 99,</div>
<div style="text-indent:-4ex;margin-left:28ex;">MathsMark = 99</div>
<div style="text-indent:-4ex;margin-left:24ex;">});</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>Now the data structures are complete, add the following<strong> .xaml</strong> markup</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Window</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Class</span><span style="color:#0000ff;">=&quot;ExamApp.MainWindow&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">x</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Converters</span><span style="color:#0000ff;">=&quot;clr-namespace:ExamApp.Converters&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Title</span><span style="color:#0000ff;">=&quot;MainWindow&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Width</span><span style="color:#0000ff;">=&quot;525&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Height</span><span style="color:#0000ff;">=&quot;350&quot;&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Converters</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">MarkToGradeConverter</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&quot;MarkToGradeConverter&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListView</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;studentsListBox&quot;&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListView.View</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridView</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridViewColumn</span><span style="color:#ff0000;"> Width</span><span style="color:#0000ff;">=&quot;120&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">DisplayMemberBinding</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=FirstName}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">Header</span><span style="color:#0000ff;">=&quot;Name&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridViewColumn</span><span style="color:#ff0000;"> Width</span><span style="color:#0000ff;">=&quot;120&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">DisplayMemberBinding</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=Surname}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">Header</span><span style="color:#0000ff;">=&quot;Surname&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridViewColumn</span><span style="color:#ff0000;"> Width</span><span style="color:#0000ff;">=&quot;40&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">DisplayMemberBinding</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=ArtAndDesignMark,</span></div>
<div style="text-indent:-4ex;margin-left:66ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> MarkToGradeConverter</span><span style="color:#0000ff;">}}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">Header</span><span style="color:#0000ff;">=&quot;Art&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridViewColumn</span><span style="color:#ff0000;"> Width</span><span style="color:#0000ff;">=&quot;60&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">DisplayMemberBinding</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=ScienceMark,</span></div>
<div style="text-indent:-4ex;margin-left:66ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> MarkToGradeConverter</span><span style="color:#0000ff;">}}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">Header</span><span style="color:#0000ff;">=&quot;Science&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">GridViewColumn</span><span style="color:#ff0000;"> Width</span><span style="color:#0000ff;">=&quot;50&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">DisplayMemberBinding</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=MathsMark,</span></div>
<div style="text-indent:-4ex;margin-left:66ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> MarkToGradeConverter</span><span style="color:#0000ff;">}}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:35ex;"><span style="color:#ff0000;">Header</span><span style="color:#0000ff;">=&quot;Maths&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">GridView</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ListView.View</span><span style="color:#0000ff;">&gt;</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ListView</span><span style="color:#0000ff;">&gt;</span></div>
<div>&#160;</div>
<div><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Window</span><span style="color:#0000ff;">&gt;</span></div>
<div>&#160;</div>
</p></div>
<p>Add the following code to the constructor of the main window</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System.Windows;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> ExamApp</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Interaction logic for MainWindow.xaml</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">MainWindow</span> : <span style="color:#2b91af;">Window</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> MainWindow()</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;">InitializeComponent();</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">this</span>.studentsListBox.ItemsSource = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Students</span>();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>You should have the following</p>
<p><a href="http://castalian.files.wordpress.com/2011/06/students.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="Students" border="0" alt="Students" src="http://castalian.files.wordpress.com/2011/06/students_thumb.png?w=529&#038;h=354" width="529" height="354" /></a></p>
<p>As you can see it is quite a simple and pretty reusable way to use ranges in your application.</p>
<p>The source code for this simple example is available <a href="http://cid-f9b0e018a02bb3d4.skydrive.live.com/redir.aspx?resid=F9B0E018A02BB3D4!126">here</a></p>
<br />Filed under: <a href='http://lukhezo.com/category/silverlight/'>Silverlight</a>, <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a>, <a href='http://lukhezo.com/category/windows-phone-7/'>Windows Phone 7</a>, <a href='http://lukhezo.com/category/wpf/'>WPF</a> Tagged: <a href='http://lukhezo.com/tag/c/'>C#</a>, <a href='http://lukhezo.com/tag/calculate/'>Calculate</a>, <a href='http://lukhezo.com/tag/determine/'>Determine</a>, <a href='http://lukhezo.com/tag/limit/'>Limit</a>, <a href='http://lukhezo.com/tag/range/'>Range</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1102&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/06/02/calculate-rangelimit-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/06/students_thumb.png" medium="image">
			<media:title type="html">Students</media:title>
		</media:content>
	</item>
		<item>
		<title>Zoomable autosizing canvas in WPF</title>
		<link>http://lukhezo.com/2011/05/22/zoomable-autosizing-canvas-in-wpf/</link>
		<comments>http://lukhezo.com/2011/05/22/zoomable-autosizing-canvas-in-wpf/#comments</comments>
		<pubDate>Sun, 22 May 2011 19:55:48 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[child items]]></category>
		<category><![CDATA[scaleable]]></category>
		<category><![CDATA[zoom]]></category>
		<category><![CDATA[zooming]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/2011/05/22/zoomable-autosizing-canvas-in-wpf/</guid>
		<description><![CDATA[I have created a control for a project I am working on that uses a canvas to draw graph ticks for a custom scale, but ran into the limitation of the canvas control where child items are not scaled accordingly when the height (or width) of the canvas changes. Luckily, this is quite a straightforward [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1094&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have created a control for a project I am working on that uses a canvas to draw graph ticks for a custom scale, but ran into the limitation of the canvas control where child items are not scaled accordingly when the height (or width) of the canvas changes. Luckily, this is quite a straightforward problem to fix, but I just could not seem to find either the correct search terms to enter in Google or locating code samples in books that demonstrated how to resolve the issue. I have just knocked this code up this afternoon so it is not perfect, but should suffice should you require something similar.</p>
<p><a href="http://castalian.files.wordpress.com/2011/05/small.png"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="Small" border="0" alt="Small" src="http://castalian.files.wordpress.com/2011/05/small_thumb.png?w=304&#038;h=311" width="304" height="311" /></a></p>
<p><a href="http://castalian.files.wordpress.com/2011/05/large.png"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="Large" border="0" alt="Large" src="http://castalian.files.wordpress.com/2011/05/large_thumb.png?w=257&#038;h=819" width="257" height="819" /></a></p>
<p>In order to resolve the problem you will need to create a custom canvas and override both <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.arrangeoverride.aspx">MeasureOverride and ArrangeOveride</a> incorporating any custom logic you require in these methods.</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System.Windows;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Controls;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Shapes;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> AutosizingCanvasApp.Controls</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">TickCanvas</span> : <span style="color:#2b91af;">Canvas</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">// Vertical axis where the X value is never changed</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">double</span> X = 0;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">private</span> <span style="color:#2b91af;">Size</span> initialSize;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#008000;">// Override the default Measure method of Panel</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#2b91af;">Size</span> MeasureOverride(<span style="color:#2b91af;">Size</span> availableSize)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> canvasDesiredSize = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Size</span>();</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// In our example, we just have one child. </span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#008000;">// Report that our canvas requires just the size of its only child.</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">UIElement</span> child <span style="color:#0000ff;">in</span> InternalChildren)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">child.Measure(availableSize);</div>
<div style="text-indent:-4ex;margin-left:20ex;">canvasDesiredSize = child.DesiredSize;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> canvasDesiredSize;</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#2b91af;">Size</span> ArrangeOverride(<span style="color:#2b91af;">Size</span> finalSize)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">if</span> (initialSize.Height == 0)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;">initialSize = finalSize;</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">var</span> ratio = finalSize.Height / initialSize.Height;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> index = 0; index &lt; <span style="color:#0000ff;">this</span>.InternalChildren.Count; index++)</div>
<div style="text-indent:-4ex;margin-left:16ex;">{</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#2b91af;">UIElement</span> child = <span style="color:#0000ff;">this</span>.InternalChildren[index];</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;"><span style="color:#0000ff;">double</span> y = ((<span style="color:#2b91af;">Line</span>)child).Y1;</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:20ex;">child.Arrange(finalSize.Height &gt; initialSize.Height</div>
<div style="text-indent:-4ex;margin-left:38ex;">? <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Rect</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Point</span>(X, (ratio * y) &#8211; y), child.DesiredSize)</div>
<div style="text-indent:-4ex;margin-left:38ex;">: <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Rect</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Point</span>(X, 0), child.DesiredSize));</div>
<div style="text-indent:-4ex;margin-left:16ex;">}</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> finalSize; <span style="color:#008000;">// Returns the final Arranged size</span></div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
</p></div>
<p>In the .xaml I have a border with the custom canvas as a child that have some Line objects all affecting the Y axis position.</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Window</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Class</span><span style="color:#0000ff;">=&quot;AutosizingCanvasApp.MainWindow&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">x</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">controls</span><span style="color:#0000ff;">=&quot;clr-namespace:AutosizingCanvasApp.Controls&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">local</span><span style="color:#0000ff;">=&quot;clr-namespace:AutosizingCanvasApp.Converters&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Title</span><span style="color:#0000ff;">=&quot;mainWindow&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Width</span><span style="color:#0000ff;">=&quot;300&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">Height</span><span style="color:#0000ff;">=&quot;768&quot;</span></div>
<div style="text-indent:-4ex;margin-left:11ex;"><span style="color:#ff0000;">WindowStartupLocation</span><span style="color:#0000ff;">=&quot;CenterScreen&quot;&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">local</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">BorderHeightValueConverter</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&quot;BorderHeightValueConverter&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">local</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">LinePositionConverter</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&quot;LinePositionConverter&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Border</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;mainBorder&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">Width</span><span style="color:#0000ff;">=&quot;94&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">Height</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainWindow,</span></div>
<div style="text-indent:-4ex;margin-left:32ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:32ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> BorderHeightValueConverter</span><span style="color:#0000ff;">}}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">Margin</span><span style="color:#0000ff;">=&quot;50&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">BorderBrush</span><span style="color:#0000ff;">=&quot;Black&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">BorderThickness</span><span style="color:#0000ff;">=&quot;2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:15ex;"><span style="color:#ff0000;">ToolTip</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:33ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight}&quot;&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">controls</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">TickCanvas</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;myCanvas&quot;&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Line</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;line1&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Stroke</span><span style="color:#0000ff;">=&quot;Red&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">StrokeThickness</span><span style="color:#0000ff;">=&quot;2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">ToolTip</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=line1,</span></div>
<div style="text-indent:-4ex;margin-left:39ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Y1}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X1</span><span style="color:#0000ff;">=&quot;0&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X2</span><span style="color:#0000ff;">=&quot;60&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y1</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>2<span style="color:#0000ff;">}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y2</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>2<span style="color:#0000ff;">}&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Line</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;line2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Stroke</span><span style="color:#0000ff;">=&quot;Blue&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">StrokeThickness</span><span style="color:#0000ff;">=&quot;2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">ToolTip</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=line2,</span></div>
<div style="text-indent:-4ex;margin-left:39ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Y1}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X1</span><span style="color:#0000ff;">=&quot;0&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X2</span><span style="color:#0000ff;">=&quot;60&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y1</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>4<span style="color:#0000ff;">}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y2</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>4<span style="color:#0000ff;">}&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Line</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;line3&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Stroke</span><span style="color:#0000ff;">=&quot;Green&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">StrokeThickness</span><span style="color:#0000ff;">=&quot;2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">ToolTip</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=line3,</span></div>
<div style="text-indent:-4ex;margin-left:39ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Y1}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X1</span><span style="color:#0000ff;">=&quot;0&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X2</span><span style="color:#0000ff;">=&quot;60&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y1</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>6<span style="color:#0000ff;">}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y2</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>6<span style="color:#0000ff;">}&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Line</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;line4&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Stroke</span><span style="color:#0000ff;">=&quot;Purple&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">StrokeThickness</span><span style="color:#0000ff;">=&quot;2&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">ToolTip</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=line4,</span></div>
<div style="text-indent:-4ex;margin-left:39ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Y1}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X1</span><span style="color:#0000ff;">=&quot;0&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">X2</span><span style="color:#0000ff;">=&quot;60&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y1</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>8<span style="color:#0000ff;">}&quot;</span></div>
<div style="text-indent:-4ex;margin-left:21ex;"><span style="color:#ff0000;">Y2</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=mainBorder,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=ActualHeight,</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">Converter</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> LinePositionConverter</span><span style="color:#0000ff;">},</span></div>
<div style="text-indent:-4ex;margin-left:34ex;"><span style="color:#ff0000;">ConverterParameter</span><span style="color:#0000ff;">=</span>0<span style="color:#0000ff;">.</span>8<span style="color:#0000ff;">}&quot; /&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">controls</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">TickCanvas</span><span style="color:#0000ff;">&gt;</span></div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Border</span><span style="color:#0000ff;">&gt;</span></div>
<div><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Window</span><span style="color:#0000ff;">&gt;</span></div>
<div><span style="color:#0000ff;"></span></div>
</p></div>
<p>I also have a couple of converters that converts the height of the border as that changes when you r<em>esize the parent window</em> and a couple of converters that reposition the line objects in the canvas. Be aware that the border itself has a thickness, so you will need to incorporate that in any sizing but has been omitted here so the lines may be a couple of pixels off.</p>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div><span style="color:#0000ff;">using</span> System.Globalization;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Data;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> AutosizingCanvasApp.Converters</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">LinePositionConverter</span> : <span style="color:#2b91af;">IValueConverter</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#region</span> Implementation of IValueConverter</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Converts a value. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> A converted value. If the method returns null, the valid null value is used.</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;</span><span style="color:#008000;">The value produced by the binding source.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;targetType&quot;&gt;</span><span style="color:#008000;">The type of the binding target property.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;parameter&quot;&gt;</span><span style="color:#008000;">The converter parameter to use.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;culture&quot;&gt;</span><span style="color:#008000;">The culture to use in the converter.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> Convert(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> ((<span style="color:#0000ff;">double</span>) value)*<span style="color:#0000ff;">double</span>.Parse(parameter.ToString());</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Converts a value. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> A converted value. If the method returns null, the valid null value is used.</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;</span><span style="color:#008000;">The value that is produced by the binding target.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;targetType&quot;&gt;</span><span style="color:#008000;">The type to convert to.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;parameter&quot;&gt;</span><span style="color:#008000;">The converter parameter to use.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;culture&quot;&gt;</span><span style="color:#008000;">The culture to use in the converter.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ConvertBack(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#00008b;">NotImplementedException</span>();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#endregion</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
</p></div>
<div style="font-family:monospace;">
<div><span style="color:#0000ff;">using</span> System;</div>
<div><span style="color:#0000ff;">using</span> System.Globalization;</div>
<div><span style="color:#0000ff;">using</span> System.Windows.Data;</div>
<div>&#160;</div>
<div><span style="color:#0000ff;">namespace</span> AutosizingCanvasApp.Converters</div>
<div>{</div>
<div style="text-indent:-4ex;margin-left:8ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">BorderHeightValueConverter</span> : <span style="color:#2b91af;">IValueConverter</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">{</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#region</span> Implementation of IValueConverter</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Converts a value. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> A converted value. If the method returns null, the valid null value is used.</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;</span><span style="color:#008000;">The value produced by the binding source.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;targetType&quot;&gt;</span><span style="color:#008000;">The type of the binding target property.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;parameter&quot;&gt;</span><span style="color:#008000;">The converter parameter to use.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;culture&quot;&gt;</span><span style="color:#008000;">The culture to use in the converter.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> Convert(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">return</span> (((<span style="color:#0000ff;">double</span>)value)*0.7);</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Converts a value. </span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> A converted value. If the method returns null, the valid null value is used.</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;/returns&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#808080;">///</span> <span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;</span><span style="color:#008000;">The value that is produced by the binding target.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;targetType&quot;&gt;</span><span style="color:#008000;">The type to convert to.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;parameter&quot;&gt;</span><span style="color:#008000;">The converter parameter to use.</span><span style="color:#808080;">&lt;/param&gt;&lt;param name=&quot;culture&quot;&gt;</span><span style="color:#008000;">The culture to use in the converter.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ConvertBack(<span style="color:#0000ff;">object</span> value, <span style="color:#2b91af;">Type</span> targetType, <span style="color:#0000ff;">object</span> parameter, <span style="color:#2b91af;">CultureInfo</span> culture)</div>
<div style="text-indent:-4ex;margin-left:12ex;">{</div>
<div style="text-indent:-4ex;margin-left:16ex;"><span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#00008b;">NotImplementedException</span>();</div>
<div style="text-indent:-4ex;margin-left:12ex;">}</div>
<div>&#160;</div>
<div style="text-indent:-4ex;margin-left:12ex;"><span style="color:#0000ff;">#endregion</span></div>
<div style="text-indent:-4ex;margin-left:8ex;">}</div>
<div>}</div>
<div>&#160;</div>
</p></div>
<p>The source code is for this example is available <a href="http://cid-f9b0e018a02bb3d4.office.live.com/self.aspx/Documents/AutosizingCanvasApp.rar">here</a>.</p>
<br />Filed under: <a href='http://lukhezo.com/category/silverlight/'>Silverlight</a>, <a href='http://lukhezo.com/category/windows-phone-7/'>Windows Phone 7</a>, <a href='http://lukhezo.com/category/wpf/'>WPF</a> Tagged: <a href='http://lukhezo.com/tag/canvas/'>canvas</a>, <a href='http://lukhezo.com/tag/child-items/'>child items</a>, <a href='http://lukhezo.com/tag/scaleable/'>scaleable</a>, <a href='http://lukhezo.com/tag/wpf/'>WPF</a>, <a href='http://lukhezo.com/tag/zoom/'>zoom</a>, <a href='http://lukhezo.com/tag/zooming/'>zooming</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1094/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1094/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1094/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1094&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/05/22/zoomable-autosizing-canvas-in-wpf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/small_thumb.png" medium="image">
			<media:title type="html">Small</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/large_thumb.png" medium="image">
			<media:title type="html">Large</media:title>
		</media:content>
	</item>
		<item>
		<title>Reinventing the wheel</title>
		<link>http://lukhezo.com/2011/05/22/reinventing-the-wheel/</link>
		<comments>http://lukhezo.com/2011/05/22/reinventing-the-wheel/#comments</comments>
		<pubDate>Sun, 22 May 2011 11:37:45 +0000</pubDate>
		<dc:creator>Ira Lukhezo</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">https://castalian.wordpress.com/2011/05/22/reinventing-the-wheel/</guid>
		<description><![CDATA[One of&#160; the most significant challenges as a developer &#8211; especially when working as a consultant (though not mutually exclusive) -&#160; is the need to understand a completely alien software system very very quickly, and to be productive in adding new functionality to that system, consequently value, to whatever the project may be. One must [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1089&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of&#160; the most significant challenges as a developer &#8211; especially when working as a consultant (though not mutually exclusive) -&#160; is the need to understand a completely alien software system very very quickly, and to be productive in adding new functionality to that system, consequently value, to whatever the project may be. One must cultivate procedures and practices that allow for the rapid addition of development requests that are fit for purpose.</p>
<p>Probably the biggest hindrance in all software development is developers that suffer from “not invented here, I didn&#8217;t write it” syndrome. Frequently, I encounter developers that cannot work on a piece of code without completely re-writing it, frequently either ending up with exactly the same functionality as the code they replaced or reinventing a square wheel i.e. one finds that one is actually are worse off from a code maintainability standpoint.</p>
<p>The role of any Software Architect or Team Lead, is primarily to ensure that the overall architecture of&#160; a software system is sound i.e. well designed and robust, lending itself to the addition of new functionality without breaking or changing existing code. This is by no means easy, and the challenges of building software systems are well known, but software architecture is now no longer in the dark ages, established design patterns that resolve most system issues are abundantly available. This most important part of any software application, I cannot but help but to draw attention to that fact, getting this wrong, with the knowledge that you will have developers of varying abilities and skills during the lifetime of the application, is all but a recipe for failure. </p>
<h3>Design</h3>
<p>The architecture of the application must be a <em>policy </em>and this must be enforced. The architecture must make it difficult for developers to just do what they want by requiring that they have to either take a few extra steps in their algorithm or think differently (but the same for everyone else) in resolving whatever they are tasked with. This however, must be a justified design decision, and not loose coupling just for the sake of loose coupling i.e.using interfaces between layers for example, usually forces composability from developers where they would usually use direct call backs. I have seen systems quickly turn into big balls of mud because of this, and the larger the system that bigger the ball of mud.</p>
<h3>&#160;</h3>
<p><a href="http://castalian.files.wordpress.com/2011/05/7299iuz6qug2mn.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="7299iuz6qug2mn" border="0" alt="7299iuz6qug2mn" src="http://castalian.files.wordpress.com/2011/05/7299iuz6qug2mn_thumb.jpg?w=404&#038;h=252" width="404" height="252" /></a></p>
<p>Since the overall architecture has been formalised, what remains is in adding the functionality of different rooms. The Drawing Room in the home above, must have all the features and components that allow it to function as such. This is analogous to any feature in a software system. It is difficult for your typical programmer to see a software system in its completeness like the house above, so it is the Architects responsibility to ensure they are aware of what is required of them. Most developers don’t like being restricted in how they are allowed to solve a problem, because most developers are not Architects, and are seldom able to design a coherent and robust system. Rewriting code someone else has written for the most part is usually an exercise in absolute futility. One cannot count the times that programmers habitually duplicate functionally that already exists, and how negatively this impacts a projects progression. It is very easy to lose weeks and even months of development time, all because developers were unable to augment existing code that was <em>already working</em>.</p>
<h3>Sawdust</h3>
<p>If you task a typical programmer to design and develop a piece of functionality in an application, that has specific functionality, the results are often surprising. Requirements for software usually result in a single piece of functionality, lets take a chair for example.</p>
<p><a href="http://castalian.files.wordpress.com/2011/05/11151bguz98uw24.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="11151bguz98uw24" border="0" alt="11151bguz98uw24" src="http://castalian.files.wordpress.com/2011/05/11151bguz98uw24_thumb.jpg?w=404&#038;h=270" width="404" height="270" /></a></p>
<p>I am certain the audience knows what the prime functionality of a chair is so I shall not elaborate, why should software be different? The objective for any programmer is to get the requirements realised in as short a time, but as functional as possible. Most programmers never create a chair that is simple and functional but end up with this.</p>
<p><a href="http://castalian.files.wordpress.com/2011/05/9481t87166dxi7.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="9481t87166dxi7" border="0" alt="9481t87166dxi7" src="http://castalian.files.wordpress.com/2011/05/9481t87166dxi7_thumb.jpg?w=404&#038;h=276" width="404" height="276" /></a></p>
<p>Really skilled developers will give you something like a Futon</p>
<p><a href="http://castalian.files.wordpress.com/2011/05/product_detailed_image_29785_21.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;padding-top:0;" title="product_detailed_image_29785_21" border="0" alt="product_detailed_image_29785_21" src="http://castalian.files.wordpress.com/2011/05/product_detailed_image_29785_21_thumb.jpg?w=404&#038;h=324" width="404" height="324" /></a></p>
<p>Most developers spend and incredible amount of time creating sawdust, separating logic and creating loosely coupled components when a lot of the time this is not required. Developers often try and use their glass ball imagining future use cases and try to always create components that can be extended to have dual or many other uses e.g. the Futon above. This is wrong! Oftentimes, all you want is a chair. All the requirements in the software specification ask for is a chair and the developer ended up creating a Futon, then are perplexed when they receive lukewarm responses or even complaints that they have designed something that was not required.</p>
<p><a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single responsibility</a> is frequently misunderstood, and is a prime reason that programmers rewrite existing code a lot . Usually the argument is that “I had to split the code into this class, and that class, and the other class because I wanted to create a software component where single responsibility was as clear as possible”. The result almost always is sawdust, and fine grained sawdust at that, because well architected software systems usually just need software components like the chair. It is sometimes hard for intelligent developers to accept that a lot of the code they write at times can be solved very simply, their overactive minds end up creating a software system that is bloated with classes and code that will never ever be used because <em><a href="http://en.wikipedia.org/wiki/You_ain't_gonna_need_it">you aren&#8217;t going to need it</a>.</em></p>
<h3>The Graduate</h3>
<p>It seems almost unfair to include graduates as prime candidates for reinventing wheels, but they almost always are the people that do this the most, including Masters up to and including PhD students.</p>
<p><a href="http://castalian.files.wordpress.com/2011/05/21585e5556z58a1.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;margin-right:auto;padding-top:0;border-width:0;" title="21585e5556z58a1" border="0" alt="21585e5556z58a1" src="http://castalian.files.wordpress.com/2011/05/21585e5556z58a1_thumb.jpg?w=404&#038;h=304" width="404" height="304" /></a></p>
<p>In any software team, you need bright and talented individuals, especially where you are solving domain specific problems. Their importance cannot be overstated here, but with this comes a lot of baggage. It can be very challenging managing a team of incredibly gifted developers. Inexperienced developers of any academic background can also be a drain in resources. A simple problem is usually solved by using the most complex algorithm, when something simpler was required. It seems inconceivable, but most of the hardest problems to manage in complex software systems are usually a result of the smartest people in a teams undue influence. If people are very smart they are hardly ever questioned to justify their choices. I have encountered software where you are told that users hate using the application because “you require a PhD to use the software”. This complexity invariably stems from the source code, thus, it is important to accept that smart individuals always find ways of solving the most difficult problems, but can also be the reason why some problems in complex software systems are difficult to solve because simplicity is seldom a prerequisite in resolving issues.</p>
<p>The long-and-the-short-of-it is that the goal of any software is for people to use it, and most success is based on simplicity and easy of use, which also is true for maintainable software systems. Working software seldom needs to be completely re-written because it <em>works</em>. Improvements can always be made to any software because there are no perfect software systems, so save yourself the time and effort by learning other peoples code rather than rewriting it, this will allow you to solve more problems, add more features and provide real value to a project.</p>
<br />Filed under: <a href='http://lukhezo.com/category/general/'>General</a>, <a href='http://lukhezo.com/category/tips-tricks/'>Tips &amp; Tricks</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/castalian.wordpress.com/1089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/castalian.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/castalian.wordpress.com/1089/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lukhezo.com&amp;blog=2467070&amp;post=1089&amp;subd=castalian&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lukhezo.com/2011/05/22/reinventing-the-wheel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">castalian</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/7299iuz6qug2mn_thumb.jpg" medium="image">
			<media:title type="html">7299iuz6qug2mn</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/11151bguz98uw24_thumb.jpg" medium="image">
			<media:title type="html">11151bguz98uw24</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/9481t87166dxi7_thumb.jpg" medium="image">
			<media:title type="html">9481t87166dxi7</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/product_detailed_image_29785_21_thumb.jpg" medium="image">
			<media:title type="html">product_detailed_image_29785_21</media:title>
		</media:content>

		<media:content url="http://castalian.files.wordpress.com/2011/05/21585e5556z58a1_thumb.jpg" medium="image">
			<media:title type="html">21585e5556z58a1</media:title>
		</media:content>
	</item>
	</channel>
</rss>
