<?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>C# Disciples &#187; .Net 3.5</title>
	<atom:link href="http://marlongrech.wordpress.com/category/net-35/feed/" rel="self" type="application/rss+xml" />
	<link>http://marlongrech.wordpress.com</link>
	<description>my life in Avalon ....</description>
	<lastBuildDate>Thu, 15 Oct 2009 09:19:13 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='marlongrech.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/0c938eec94507e7222db1a9e8ad7ba9e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>C# Disciples &#187; .Net 3.5</title>
		<link>http://marlongrech.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://marlongrech.wordpress.com/osd.xml" title="C# Disciples" />
		<item>
		<title>Playing with Shaders: Creating a water effect</title>
		<link>http://marlongrech.wordpress.com/2009/08/27/playing-with-shaders-creating-a-water-effect/</link>
		<comments>http://marlongrech.wordpress.com/2009/08/27/playing-with-shaders-creating-a-water-effect/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 15:06:03 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[pixel shaders]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/08/27/playing-with-shaders-creating-a-water-effect/</guid>
		<description><![CDATA[Lately I’ve been looking at a really cool project on Codeplex for Pixel Shaders. The set of shaders in this library is awsome and really easy to use in you application….
All you need to do to consume any Shader is add a reference to the WPFShaderEffectLibrary class library and you can start using the shaders [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=355&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Lately I’ve been looking at a really cool project on <a href="http://www.codeplex.com/wpffx" target="_blank">Codeplex for Pixel Shaders</a>. The set of shaders in this library is awsome and really easy to use in you application….</p>
<p>All you need to do to consume any Shader is add a reference to the WPFShaderEffectLibrary class library and you can start using the shaders in XAML.What is really important is that you install the <a href="http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=14962#DownloadId=40167" target="_blank">Shaders Build Task</a> before trying to build the shader library (if you just add a reference to the ready build dlls you don’t even need to do this).</p>
<p>&#160;</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/08/water.png"><img style="display:inline;border-width:0;" title="Water" border="0" alt="Water" src="http://marlongrech.files.wordpress.com/2009/08/water_thumb.png?w=644&#038;h=389" width="644" height="389" /></a> </p>
<p>So let’s have a look at how we can use shaders to simulate water on screen.</p>
<p>This is very easy all we need is a ripple shader effect. so something like this </p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;Window.Resources&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>    &lt;Storyboard x:Key=<span style="color:#006080;">&quot;waterAnimMain&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>        &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Phase&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;10&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;Stop&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>        &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Amplitude&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;0.6&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>        &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Frequency&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;30&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.5&quot;</span> FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>    &lt;/Storyboard&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span> &lt;/Window.Resources&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span> &lt;Grid Background=<span style="color:#006080;">&quot;Transparent&quot;</span> &gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>    &lt;Border&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>        &lt;Border.Background&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>            &lt;LinearGradientBrush EndPoint=<span style="color:#006080;">&quot;0.93,0.925&quot;</span> StartPoint=<span style="color:#006080;">&quot;0.094,0.125&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>                &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF272788&quot;</span> Offset=<span style="color:#006080;">&quot;0.112&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>                &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF090916&quot;</span> Offset=<span style="color:#006080;">&quot;0.721&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>                &lt;GradientStop Color=<span style="color:#006080;">&quot;#FE222267&quot;</span> Offset=<span style="color:#006080;">&quot;0.28&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>                &lt;GradientStop Color=<span style="color:#006080;">&quot;#FE131339&quot;</span> Offset=<span style="color:#006080;">&quot;0.453&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>                &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF04040A&quot;</span> Offset=<span style="color:#006080;">&quot;0.974&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>            &lt;/LinearGradientBrush&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>        &lt;/Border.Background&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>    </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>    &lt;/Border&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span> &lt;/Grid&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span> &lt;Window.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>    &lt;shader:RippleEffect Amplitude=<span style="color:#006080;">&quot;0&quot;</span> Frequency=<span style="color:#006080;">&quot;0&quot;</span> Phase=<span style="color:#006080;">&quot;0&quot;</span> x:Name=<span style="color:#006080;">&quot;rippleMain&quot;</span>  /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span> &lt;/Window.Effect&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>This is our Shader</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;shader:RippleEffect Amplitude=<span style="color:#006080;">&quot;0&quot;</span> Frequency=<span style="color:#006080;">&quot;0&quot;</span> Phase=<span style="color:#006080;">&quot;0&quot;</span> x:Name=<span style="color:#006080;">&quot;rippleMain&quot;</span>  /&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>And we animate this shader like so to make the effect of water</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;Storyboard x:Key=<span style="color:#006080;">&quot;waterAnimMain&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>      &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Phase&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;10&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;Stop&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>      &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Amplitude&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;0.6&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>      &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Frequency&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;30&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.5&quot;</span> FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>  &lt;/Storyboard&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>We invoke this animation by having a DispatcherTimer trigger the animation every now and then and changing the Center property of the Shader to a random point from 0 to 1.</p>
<p>This already gets us very close but it still does not feel like real water. The trick is to animate to ripple effects at the same time. This will make the ripple effects expand together and thus making the control which has the shader applied look like water. Yet the problem is that you can only apply ONE shader per control. BUT you can have another shader on the parent control and that would still apply the shader on all children (<a href="http://marlongrech.wordpress.com/2008/05/15/effects-in-net-sp1-for-wpf/" target="_blank">I explain this in this article</a>).</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;Window.Resources&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     &lt;Storyboard x:Key=<span style="color:#006080;">&quot;waterAnimMain&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Phase&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;10&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;Stop&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Amplitude&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;0.6&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleMain&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Frequency&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;30&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.5&quot;</span> FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>     &lt;/Storyboard&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>     &lt;Storyboard x:Key=<span style="color:#006080;">&quot;waterAnimSub&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleSub&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Phase&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;10&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;Stop&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleSub&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Amplitude&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;0.6&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.0&quot;</span>  FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>         &lt;DoubleAnimation Storyboard.TargetName=<span style="color:#006080;">&quot;rippleSub&quot;</span> Storyboard.TargetProperty=<span style="color:#006080;">&quot;Frequency&quot;</span> To=<span style="color:#006080;">&quot;0&quot;</span> From=<span style="color:#006080;">&quot;30&quot;</span> Duration=<span style="color:#006080;">&quot;0:0:2.5&quot;</span> FillBehavior=<span style="color:#006080;">&quot;HoldEnd&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>     &lt;/Storyboard&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span> &lt;/Window.Resources&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span> &lt;Grid Background=<span style="color:#006080;">&quot;Transparent&quot;</span> &gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>     &lt;Border&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>         &lt;Border.Background&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>             &lt;LinearGradientBrush EndPoint=<span style="color:#006080;">&quot;0.93,0.925&quot;</span> StartPoint=<span style="color:#006080;">&quot;0.094,0.125&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>                 &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF272788&quot;</span> Offset=<span style="color:#006080;">&quot;0.112&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>                 &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF090916&quot;</span> Offset=<span style="color:#006080;">&quot;0.721&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>                 &lt;GradientStop Color=<span style="color:#006080;">&quot;#FE222267&quot;</span> Offset=<span style="color:#006080;">&quot;0.28&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>                 &lt;GradientStop Color=<span style="color:#006080;">&quot;#FE131339&quot;</span> Offset=<span style="color:#006080;">&quot;0.453&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span>                 &lt;GradientStop Color=<span style="color:#006080;">&quot;#FF04040A&quot;</span> Offset=<span style="color:#006080;">&quot;0.974&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>             &lt;/LinearGradientBrush&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span>         &lt;/Border.Background&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>         &lt;Border.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span>             &lt;shader:RippleEffect Amplitude=<span style="color:#006080;">&quot;0&quot;</span> Frequency=<span style="color:#006080;">&quot;0&quot;</span> Phase=<span style="color:#006080;">&quot;0&quot;</span> x:Name=<span style="color:#006080;">&quot;ripple&quot;</span> Center=<span style="color:#006080;">&quot;{Binding ElementName=main, Path=(local:MouseBehaviour.LastMouseUp)}&quot;</span> /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum30">  30:</span>         &lt;/Border.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum31">  31:</span>     &lt;/Border&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum32">  32:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum33">  33:</span>     &lt;Grid.Triggers&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum34">  34:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum35">  35:</span>         &lt;EventTrigger RoutedEvent=<span style="color:#006080;">&quot;UIElement.MouseUp&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum36">  36:</span>             &lt;BeginStoryboard Storyboard=<span style="color:#006080;">&quot;{StaticResource waterAnim}&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum37">  37:</span>             &lt;BeginStoryboard Storyboard=<span style="color:#006080;">&quot;{StaticResource waterAnimMain}&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum38">  38:</span>         &lt;/EventTrigger&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum39">  39:</span>     &lt;/Grid.Triggers&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum40">  40:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum41">  41:</span>     &lt;Grid.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum42">  42:</span>         &lt;shader:RippleEffect Amplitude=<span style="color:#006080;">&quot;0&quot;</span> Frequency=<span style="color:#006080;">&quot;0&quot;</span> Phase=<span style="color:#006080;">&quot;0&quot;</span> x:Name=<span style="color:#006080;">&quot;rippleSub&quot;</span>  /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum43">  43:</span>     &lt;/Grid.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum44">  44:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum45">  45:</span> &lt;/Grid&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum46">  46:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum47">  47:</span> &lt;Window.Effect&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum48">  48:</span>     &lt;shader:RippleEffect Amplitude=<span style="color:#006080;">&quot;0&quot;</span> Frequency=<span style="color:#006080;">&quot;0&quot;</span> Phase=<span style="color:#006080;">&quot;0&quot;</span> x:Name=<span style="color:#006080;">&quot;rippleMain&quot;</span>  /&gt;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum49">  49:</span> &lt;/Window.Effect&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>And that’s it. Now we have a perfect water look thanks to the Codeplex Pixel Shader library <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I created a sample app that shows all this. The app also has another feature so that you can touch the water by using the mouse and the water would ripple from the point you touch (of course this is with Behaviours so that you can even reuse it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  )</p>
<p>Happy coding <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/WaterDemo.zip" target="_blank">DOWNLOAD SOURCE CODE</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/355/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=355&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/08/27/playing-with-shaders-creating-a-water-effect/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/08/water_thumb.png" medium="image">
			<media:title type="html">Water</media:title>
		</media:content>
	</item>
		<item>
		<title>Mediator v2 for MVVM WPF and Silverlight applications</title>
		<link>http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/</link>
		<comments>http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 10:06:16 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlight]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/</guid>
		<description><![CDATA[This last 2 weeks, us WPF Disciples have been talking a lot about the Mediator pattern for MVVM applications. Me and Josh Smith revised my previous Mediator implementation and found some problems with it.
Problem: There was a memory leak. Once a Mediator subscribes for one or more messages it would be kept in memory since [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=332&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This last 2 weeks, <a href="http://groups.google.com/group/wpf-disciples">us WPF Disciples</a> have been talking a lot about the <a href="http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/">Mediator pattern</a> for MVVM applications. Me and <a href="http://joshsmithonwpf.wordpress.com/">Josh Smith</a> <a href="http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/">revised</a> my previous Mediator implementation and found some problems with it.</p>
<p><strong>Problem</strong>: There was a memory leak. Once a Mediator subscribes for one or more messages it would be kept in memory since the Mediator was holding a reference to the ViewModel.     <br /><strong>Solution</strong>: We could have just exposed an unregister method so that when you call this method the Mediator would release the instance of the ViewModel. But sometimes it is hard to know when a ViewModel is going to die… So we went a step further and created the WeakAction. The WeakAction stores the instance of the ViewModel in a WeakRefence thus the ViewModel can be garbage collected without any problem and once it does get garbage collected the Mediator automatically removes that ViewModel registration from the list.</p>
<p>While we were at it we PIMPED the Mediator API <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  We added 2 approaches to subscribe to the Mediator and we also added support for (thanks to <a href="http://blogs.msdn.com/delay/default.aspx">David Anson</a> suggestion) strongly typed parameters.</p>
<p>So now the Mediator offers 2 methods of registration, a declarative way of registering and an imperative way as well.</p>
<p><strong>The Declarative approach. (Supported in WPF and Silverlight)</strong></p>
<p>This approach uses attributes so that you can decorate the messages that act as subscribers for messages.Here is an example </p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">/// Subscribe to Message1</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#008000;">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#008000;">/// &lt;param name=&quot;message&quot;&gt;The message to be passed&lt;/param&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> [MediatorMessageSink(MediatorMessages.Message1, ParameterType = <span style="color:#0000ff;">typeof</span>(SomeData))]</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Test(SomeData data)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     MessageBox.Show(data.Text + <span style="color:#006080;">&quot;\nReceived by: ColleagueA&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>So you just have to decorate your method with MediatorMessageSink attribute and the Method will get invoked every time a message is broadcasted via the Mediator. </p>
<p>As you can see the Mediator also <strong>supports strongly typed parameters</strong>. In order to do so you have to specify the ParameterType property on the attribute as shown in the sample code above. You can pass 0 or 1 parameters. If you don’t have any parameters just ignore the ParameterType (since this is an named Property and you can just not set it )</p>
<p>When using the declarative approach you <strong>must</strong> register the instance of the ViewModel to the Mediator. This is just one line of code in the ViewModel constructor. Here is how you do that</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> ColleagueA()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#008000;">//Register all decorated methods to the Mediator</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     Mediator.Register(<span style="color:#0000ff;">this</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This will inspect all methods of the ViewModel and look for the MediatorMessageSink attribute and register the appropriate methods to the Mediator. If you are going to have a base class for the ViewModels in your project and you can put this in the constructor of your base class. As simple an that.</p>
<p>&#160;</p>
<p><strong>The Imperative approach. (Supported in WPF ONLY)</strong></p>
<p>If you do not want to use the attribute based registration we offer an imperative approach where you specify a method to be registered to a specific message. This implementation is exactly as it was in the old Mediator yet now it <strong>supports strongly typed parameters</strong>.</p>
<p>Here is how you can use this in your code</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:&#39;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> ColleagueC()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#008000;">//Register a specific delegate to the Mediator</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     Mediator.Register(MediatorMessages.Message1,</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         (Action&lt;SomeData&gt;)</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         <span style="color:#0000ff;">delegate</span>(SomeData x) </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>             Test = x.Text;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             MessageBox.Show(x.Text + <span style="color:#006080;">&quot; \nReceived by: ColleagueC&quot;</span>); </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         });</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#39;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This method is supported in WPF only since in Silverlight you cannot call an anonymous method (via reflection). If you try to use this method in Silverlight you will get an InvalidOperationException saying “This method is not supported in Silverlight”.</p>
<p>&#160;</p>
<p><strong>Conclusion</strong></p>
<p>I packaged the Mediator in a class library so that you can go ahead and add a reference and reuse this library in your day to day projects.As always feedback is much appreciated.</p>
<p><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/MediatorLib.zip"><strong>Download MediatorLib + Sample application</strong></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=332&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>
	</item>
		<item>
		<title>[MVVM + Mediator + ACB = cool WPF App] &#8211; Intro</title>
		<link>http://marlongrech.wordpress.com/2009/04/02/mvvm-mediator-acb-cool-wpf-app-intro/</link>
		<comments>http://marlongrech.wordpress.com/2009/04/02/mvvm-mediator-acb-cool-wpf-app-intro/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 17:49:57 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[wpf disciples]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/04/02/mvvm-mediator-acb-cool-wpf-app-intro/</guid>
		<description><![CDATA[Finally I managed to spare some time and build a WPF sample application to showcase the use of MVVM + Mediator and also how AttachedCommandBehaviours can be used to further reduce (or better eliminate) C# code in XAML code behind files. I am also using one of my favorite WPF libraries FluidKit. In the app [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=313&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Finally I managed to spare some time and build a WPF sample application to showcase the use of MVVM + <a href="http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/" target="_blank">Mediator</a> and also how <a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" target="_blank">AttachedCommandBehaviours</a> can be used to further reduce (or better eliminate) C# code in XAML code behind files. I am also using one of my favorite WPF libraries <a href="http://www.codeplex.com/fluidkit" target="_blank">FluidKit</a>. In the app I create my own 3D transition for the FluidKit TransitionPresenter (I will be posting a whole post on this). </p>
<p>I will be posting at least 4 posts</p>
<p>- MVVM</p>
<p>- MVVM and the Mediator pattern</p>
<p>- FluidKit CoverFlow and FluidKit TransitionPresenter</p>
<p>- Using AttachedCommandBehaviours</p>
<p>Here are some teaser screenshots of the application in action <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p><a href="http://marlongrech.files.wordpress.com/2009/04/screen1.jpg"><img style="display:inline;border-width:0;" title="Screen1" border="0" alt="Screen1" src="http://marlongrech.files.wordpress.com/2009/04/screen1-thumb.jpg?w=244&#038;h=185" width="244" height="185" /></a> <a href="http://marlongrech.files.wordpress.com/2009/04/screen2.jpg"><img style="display:inline;border-width:0;" title="Screen2" border="0" alt="Screen2" src="http://marlongrech.files.wordpress.com/2009/04/screen2-thumb.jpg?w=244&#038;h=183" width="244" height="183" /></a> </p>
<p><a href="http://marlongrech.files.wordpress.com/2009/04/screen3.jpg"><img style="display:inline;border-width:0;" title="Screen3" border="0" alt="Screen3" src="http://marlongrech.files.wordpress.com/2009/04/screen3-thumb.jpg?w=244&#038;h=190" width="244" height="190" /></a> <a href="http://marlongrech.files.wordpress.com/2009/04/screen4.jpg"><img style="display:inline;border-width:0;" title="Screen4" border="0" alt="Screen4" src="http://marlongrech.files.wordpress.com/2009/04/screen4-thumb.jpg?w=244&#038;h=187" width="244" height="187" /></a></p>
<p>&#160;</p>
<h3>Setup of application environment </h3>
<p>This application uses a SQL Database so you will need an instance of SqlServer 2005 running (you can use SqlServer Express).</p>
<p>I packaged the database as a backup so all you need to do is restore the database from the SqlServer Management Studio Express (this is your easiest option). If you don’t have this I suggest you <a href="http://www.microsoft.com/downloads/details.aspx?familyid=C243A5AE-4BD1-4E3D-94B8-5A0F62BF7796&amp;displaylang=en" target="_blank">download it</a>, it’s free.</p>
<p>To restore the database file (WPFDisciplesDB.bak) follow these steps</p>
<p>- First of all <a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/WPFDisciplesDB.bak" target="_blank">download the database file from here.</a></p>
<p>- Right click on the Databases folder in the SQL Server Management Studio Express and select Restore database</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/04/step1.jpg"><img style="display:inline;border-width:0;" title="Step 1" border="0" alt="Step 1" src="http://marlongrech.files.wordpress.com/2009/04/step1-thumb.jpg?w=244&#038;h=120" width="244" height="120" /></a> </p>
<p>This will open a dialog for you. In the Restore Database dialog enter a name for the database in the “To database” text field &gt; Select “From device” radio button and browse for the WPFDisciples.bak that you downloaded. The file will appear in the list. Tick the checkbox (which is un checked by default) and press ok.</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/04/step2.jpg"><img style="display:inline;border-width:0;" title="Step 2" border="0" alt="Step 2" src="http://marlongrech.files.wordpress.com/2009/04/step2-thumb.jpg?w=542&#038;h=484" width="542" height="484" /></a>&#160;</p>
<p>Now that we have our database set up all we need to do is specify in the app.config the connection string of the new database and we are done.</p>
<p>Stayed tuned because this should be real fun <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>aaaa I nearly forgot… The data in the database is not the full version… I will add more projects from the mighty WPF Disciples as time goes by…. Will continue updating the download link here (and put a comment or something to show you that I updated the db version)</p>
<p><strong><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/MVVM%20Mediator%20ACB%20Sample%20app.zip" target="_blank">Download Full Source Code from HERE</a></strong></p>
<p><strong>Just in case you missed it, <a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/WPFDisciplesDB.bak" target="_blank">here is a copy of the database</a></strong></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/313/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=313&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/04/02/mvvm-mediator-acb-cool-wpf-app-intro/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/screen1-thumb.jpg" medium="image">
			<media:title type="html">Screen1</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/screen2-thumb.jpg" medium="image">
			<media:title type="html">Screen2</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/screen3-thumb.jpg" medium="image">
			<media:title type="html">Screen3</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/screen4-thumb.jpg" medium="image">
			<media:title type="html">Screen4</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/step1-thumb.jpg" medium="image">
			<media:title type="html">Step 1</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/04/step2-thumb.jpg" medium="image">
			<media:title type="html">Step 2</media:title>
		</media:content>
	</item>
		<item>
		<title>XBAPs Part 2: Consuming WCF services from an XBAP</title>
		<link>http://marlongrech.wordpress.com/2009/02/16/xbaps-part-2-consuming-wcf-services-from-an-xbap/</link>
		<comments>http://marlongrech.wordpress.com/2009/02/16/xbaps-part-2-consuming-wcf-services-from-an-xbap/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 20:48:00 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.0]]></category>
		<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XBAP]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/02/16/xbaps-part-2-consuming-wcf-services-from-an-xbap/</guid>
		<description><![CDATA[Introduction
One of the big improvements to XBAPs in .NET 3.5 is the ability to consume WCF services. Nowadays Service Oriented Architectures (SOAs) are very popular and used almost everywhere. This all makes sense, as SOA is very scalable, and for many cases it is the best approach to take for a project; so why not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=286&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3>Introduction</h3>
<p>One of the big improvements to XBAPs in .NET 3.5 is the ability to consume WCF services. Nowadays Service Oriented Architectures (SOAs) are very popular and used almost everywhere. This all makes sense, as SOA is very scalable, and for many cases it is the best approach to take for a project; so why not enable SOA for XBAPs as well. To tell you the truth, you could consume WCF services from XBAP in .NET 3.0, yet the XBAP had to have Full Trust, and not Partial Trust, which usually spells trouble. Anyway we don’t have to worry much about this now because .NET 3.5 enables XBAPs to consume WCF services even in Partial Trust. <a href="http://msdn.microsoft.com/en-us/library/bb412186.aspx" target="_blank">Read more about this and it’s limitations here.</a></p>
<h3>Setting up our workspace</h3>
<p>Ok so let’s start by creating a simple WCF service. Open Visual Studio and create a WCF service by clicking File &gt; New &gt; Project and then select Web &gt; WCF Service application</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/newwcf.jpg"><img style="display:inline;border-width:0;" title="NewWCF" src="http://marlongrech.files.wordpress.com/2009/02/newwcf-thumb.jpg?w=244&#038;h=189" border="0" alt="NewWCF" width="244" height="189" /></a></p>
<p>I am not going to cover the basics of how to create a WCF service, yet by default the WCF Service Visual Studio template creates a simple service for us already (called IService). So yes, if you hit F5 and run the service you will already have a service up and running. Once we have our simple service let’s create a Simple XBAP in the same solution (in order to do this please refer to <a href="http://marlongrech.wordpress.com/2009/02/05/xbaps-part-1-an-introduction-to-this-gem/" target="_blank">my previous post</a>).</p>
<p>Now that we have both the WCF service and the XBAP how are we going to make them communicate? Well, thanks to Visual Studio this is super simple. All you have to do is Right click the XBAP project, select the &#8220;Add Service Reference&#8221; option and a Dialog will pop up:</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/addservicereference.jpg"><img style="display:inline;border-width:0;" title="AddServiceReference" src="http://marlongrech.files.wordpress.com/2009/02/addservicereference-thumb.jpg?w=244&#038;h=228" border="0" alt="AddServiceReference" width="244" height="228" /></a></p>
<p><strong>Please Note:</strong> If you do not run the service you will get an error while expanding the Tree nodes of the service in this Dialog. If the service is not running Visual Studio cannot get the Metadata of the service, since this is just another EndPoint of the service. In order to run the service just Right Click on Service1.svc and select View in Browser.</p>
<p>Ok, now that we added the Service Reference, Visual Studio will go to work and generate a Proxy class for us. We will use this proxy class to communicate with the WCF service. By default the Proxy class is called &lt;Name of service&gt;Client so in our case it will be called Service1Client. This proxy class will have a method for every OperationContract we have in our service. You can also choose to create an Async version of the service. In order to do so just right click the Service1 (in the XBAP project that got created when we added the Service Reference) and select Configure Service Reference.</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/changetheservicetohaveasync.jpg"><img style="display:inline;border-width:0;" title="ChangeTheServiceToHaveASYNC" src="http://marlongrech.files.wordpress.com/2009/02/changetheservicetohaveasync-thumb.jpg?w=244&#038;h=226" border="0" alt="ChangeTheServiceToHaveASYNC" width="244" height="226" /></a></p>
<p>In this dialog check the Generate Async operations checkbox and Visual Studio will add an async version of the OperationContracts for you (Thank you VS… we love you <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<h3>The first error you’ll see</h3>
<p>Ok now that we have everything set up lets do a simple button in our XBAP and in the Click event handler call a method. Something like this….</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> Button_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span>     Service1Client client = <span style="color:#0000ff;">new</span> Service1Client();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span>     MessageBox.Show(client.GetData(1));</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span> }</pre>
</div>
</div>
<p>Let’s hit F5 and run our XBAP and see what happens.</p>
<p>AND BAMMMM a big ugly exception is thown.</p>
<p>InvalidOperationException</p>
<p>The WSHttpBinding with name WSHttpBinding failed validation because it contains a BindingElement with type System.ServiceModel.Channels.SymmetricSecurityBindingElement which is not supported in partial trust</p>
<p>which in VS looks like this</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/wcferror.jpg"><img style="display:inline;border-width:0;" title="WCFError" src="http://marlongrech.files.wordpress.com/2009/02/wcferror-thumb.jpg?w=244&#038;h=180" border="0" alt="WCFError" width="244" height="180" /></a></p>
<p>Yet I promised you guys that WCF is supported in Partial trust XBAPs…. And it is, it’s just that the default WCF EndPoint is not <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  By default a WCF Service has it’s EndPoint set up as WsHTTPBinding which is not supported in Partial trust XBAPs. In order to fix this Right click the app.config of your service and select &#8220;Edit WCF Configuration&#8221; (if this option is not available go to Tools &gt; WCF Service Configuration Editor then select open and specify the path of your App.config. Once you have the app.config opened in the editor Expand the Bindings Node and select the node under that. Open the Security tab and choose None as security level.</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/changesecuritylevelofbinding.jpg"><img style="display:inline;border-width:0;" title="ChangeSecurityLevelOfBinding" src="http://marlongrech.files.wordpress.com/2009/02/changesecuritylevelofbinding-thumb.jpg?w=244&#038;h=112" border="0" alt="ChangeSecurityLevelOfBinding" width="244" height="112" /></a></p>
<p>Once that’s done run the app again.</p>
<h3>The Second error you’ll see</h3>
<p>And this time it’s a SecurityException saying</p>
<p>Request for the permission of type &#8216;System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&#8242; failed.</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/seconderror.jpg"><img style="display:inline;border-width:0;" title="SecondError" src="http://marlongrech.files.wordpress.com/2009/02/seconderror-thumb.jpg?w=244&#038;h=84" border="0" alt="SecondError" width="244" height="84" /></a></p>
<p>In order to fix this Go to the XBAP project Properties and select Debug tab. Select the Start External Program option and enter the following exe path “C:\WINDOWS\System32\PresentationHost.exe” and in the Command Line arguments text field enter the following</p>
<p>-debug &lt;Path of XBAP&gt; –debugSecurityZoneUrl &lt;URL of service&gt;</p>
<p>in my case it was</p>
<p>-debug &#8220;D:\net resources\My Blog\XBAPAndWCF\XBAPClient\bin\Debug\XBAPClient.xbap&#8221; -debugSecurityZoneUrl <a href="http://localhost:53265/Service1.svc">http://localhost:53265/Service1.svc</a></p>
<p>[Update]</p>
<p>Thanks to <a href="http://www.julmar.com/blog/mark/">Mark Smith</a> I discovered a new an easier way of doing this. All you have to do is</p>
<p>1) Open XBAP project properties</p>
<p>2) Select Security tab</p>
<p>3) Advanced button</p>
<p>4) Check &#8220;Grant application access to site of origin&#8221; (should be checked already)</p>
<p>5) Type in the URL into the textbox where the web service is running</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/security.jpg"><img style="border-right:0;border-top:0;display:inline;border-left:0;border-bottom:0;" title="Security" src="http://marlongrech.files.wordpress.com/2009/02/security-thumb.jpg?w=244&#038;h=107" border="0" alt="Security" width="244" height="107" /></a></p>
<p>We have to do this so that we set up an envirorment for the XBAP just like it is running from the same URL of the service.</p>
<h3>Conclusion</h3>
<p>And there you have it. Now you should be armed and ready to start doing XBAPs that consume WCF service.</p>
<p><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/XBAPAndWCF.zip" target="_blank">Download sample application</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/286/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=286&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/02/16/xbaps-part-2-consuming-wcf-services-from-an-xbap/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/newwcf-thumb.jpg" medium="image">
			<media:title type="html">NewWCF</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/addservicereference-thumb.jpg" medium="image">
			<media:title type="html">AddServiceReference</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/changetheservicetohaveasync-thumb.jpg" medium="image">
			<media:title type="html">ChangeTheServiceToHaveASYNC</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/wcferror-thumb.jpg" medium="image">
			<media:title type="html">WCFError</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/changesecuritylevelofbinding-thumb.jpg" medium="image">
			<media:title type="html">ChangeSecurityLevelOfBinding</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/seconderror-thumb.jpg" medium="image">
			<media:title type="html">SecondError</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/security-thumb.jpg" medium="image">
			<media:title type="html">Security</media:title>
		</media:content>
	</item>
		<item>
		<title>XBAPs Part 1: An introduction to this Gem</title>
		<link>http://marlongrech.wordpress.com/2009/02/05/xbaps-part-1-an-introduction-to-this-gem/</link>
		<comments>http://marlongrech.wordpress.com/2009/02/05/xbaps-part-1-an-introduction-to-this-gem/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 13:35:07 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XBAP]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/02/05/xbaps-part-1-an-introduction-to-this-jem/</guid>
		<description><![CDATA[Introduction
 XBAPs (XAML Browser Application) are a really cool feature that was introduced in .NET 3.0 together with WPF. Well you can say that XBAPs are as such part of WPF because at the end of the day XBAPs are just WPF applications that can run inside the browser. One important thing to state here [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=269&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3>Introduction</h3>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/xbap.png"><img style="display:inline;border-width:0;margin:0 10px 0 0;" title="XBAP" src="http://marlongrech.files.wordpress.com/2009/02/xbap-thumb.png?w=48&#038;h=48" border="0" alt="XBAP" width="48" height="48" align="left" /></a> XBAPs (XAML Browser Application) are a really cool feature that was introduced in .NET 3.0 together with WPF. Well you can say that XBAPs are as such part of WPF because at the end of the day XBAPs are just WPF applications that can run inside the browser. One important thing to state here is that XBAPs and Silverlight have nothing to do with each other. XBAPs are full .NET enabled WPF applications, while Silverlight is a “cut down version of WPF” that runs as a browser plugin. In order to run a Silverlight enabled website you need to download the Silverlight installer(that is really really small approx 4.3 MB), if you want a client to run a XBAP the client has to have .NET installed on his machine (the Full .NET or <a href="http://msdn.microsoft.com/en-us/library/cc656912.aspx" target="_blank">Client Profile</a>).</p>
<p>Before we continue I would like to point out that this post is just an introduction to XBAPs. I will have Part 2, Part 3 and Part x that should be much more interesting to follow…. Yet if you never heard of XBAPs you should continue reading this post (and try not to fall asleep <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ).</p>
<h3>What does your Client need?</h3>
<p>As I said above, in order for a client to run your XBAP he/she needs to have .NET framework installed. It’s as if the client is going to run a Windows application. XBAPs got a set of improvements in .NET 3.5 such as support for Firefox (<a href="http://www.hanselman.com/blog/FirefoxClickOnceXBAPsAndNET35SP1.aspx" target="_blank">read more here</a>) and also support for consuming WCF service in partial trust mode (with some limitations of course but still it’s good, <a href="http://msdn.microsoft.com/en-us/library/bb412186.aspx" target="_blank">read more here</a>). So yea if you are planning on using XBAP’s I would seriously consider .NET 3.5.</p>
<p>To summarize your Client needs to have .NET installed and he can use the XBAP in Internet Explorer or if he prefers (and you are using .NET 3.5) Firefox.</p>
<h3>Some things to keep in mind …</h3>
<p>XBAPs run in the browser and give an impression of being a normal website (super cool website I must say <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) while instead they are just actual apps that never jump out of the browser, yet there is more to it… XBAP’s run in a “sandbox”, basically they run as <a href="http://www.csharp411.com/executing-code-in-partial-trust-environments/" target="_blank">Partial Trust</a> by default. What this means is that some of the things that you would assume would work in a WPF app would not work in an XBAP, for example Reflection (and also the <a href="http://marlongrech.wordpress.com/2008/05/15/effects-in-net-sp1-for-wpf/" target="_blank">new 3.5 SP1 feature Effects</a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  ). Of course there are workarounds. You can run XBAPs as Full trust mode and we will discuss how to later on in this article.</p>
<h3>Building a simple XBAP</h3>
<p>So let’s start having a look at how one can build an XBAP. In order to create a new XBAP project you have to do the usual stuff, so go to File &gt; New &gt; Project and select WPF Browser Application</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/step1.jpg"><img style="display:inline;border-width:0;" title="Step1" src="http://marlongrech.files.wordpress.com/2009/02/step1-thumb.jpg?w=244&#038;h=192" border="0" alt="Step1" width="244" height="192" /></a></p>
<p>Now that we created the project, let’s have a look at what Visual Studio created for us</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/step2.jpg"><img style="display:inline;border-width:0;" title="Step2" src="http://marlongrech.files.wordpress.com/2009/02/step2-thumb.jpg?w=244&#038;h=161" border="0" alt="Step2" width="244" height="161" /></a></p>
<p>So as you can see by default Visual Studio created more or less the same stuff it would have created for a WPF Application yet instead of using a Window it created a Page. This is very important because a Page was designed for Navigation (the back and forward button of the browser). I will discuss Navigation more later on in this article. Besides this Visual Studio also created a pfx file. The pfx file is a way how to sign the exe, by default Visual Studio creates a pfx file that it uses to sign the Click once installer for the XBAP.</p>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/signing.jpg"><img style="border-right:0;border-top:0;display:inline;border-left:0;border-bottom:0;" title="Signing" src="http://marlongrech.files.wordpress.com/2009/02/signing-thumb.jpg?w=244&#038;h=89" border="0" alt="Signing" width="244" height="89" /></a></p>
<p>The image above shows how Visual Studio generated that pfx file. Please not that there is the Computer name and username inside the pfx files (which obviously is not something you want to give to your client). Charles Perzold discusses this in <a href="http://www.charlespetzold.com/blog/2007/03/081224.html" target="_blank">more detail here</a>.</p>
<p>Ok so now that we know what Visual Studio created for us (thanks VS we love you) we can test our XBAP app (yea we can already run it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ). In order to see something on screen lets just create a button in the Page1.xaml and press F5 to run.</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> &lt;Button Content=<span style="color:#006080;">"My First XBAP <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  "</span> Height=<span style="color:#006080;">"50"</span> Width=<span style="color:#006080;">"150"</span>/&gt;</pre>
</div>
</div>
<p><a href="http://marlongrech.files.wordpress.com/2009/02/firstapp.jpg"><img style="border-right:0;border-top:0;display:inline;border-left:0;border-bottom:0;" title="FirstApp" src="http://marlongrech.files.wordpress.com/2009/02/firstapp-thumb.jpg?w=244&#038;h=132" border="0" alt="FirstApp" width="244" height="132" /></a></p>
<p>Yahhooo we have everything figured out don’t we <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  well there are a couple of other things we need to have a look at.</p>
<h3>Conclusion</h3>
<p>If you are a WPF developer you will be developing XBAP in no time. There are a couple of things you must keep in mind, like Code Access Security and other tricks like how to consume WCF service…. In my next posts I will cover these things and make sure that I pass on my knowledge (knowledge that came from a lot of frustration and hours of head banging on the table)</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/269/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=269&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/02/05/xbaps-part-1-an-introduction-to-this-gem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/xbap-thumb.png" medium="image">
			<media:title type="html">XBAP</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/step1-thumb.jpg" medium="image">
			<media:title type="html">Step1</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/step2-thumb.jpg" medium="image">
			<media:title type="html">Step2</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/signing-thumb.jpg" medium="image">
			<media:title type="html">Signing</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2009/02/firstapp-thumb.jpg" medium="image">
			<media:title type="html">FirstApp</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the WPF Web browser control in a ToolTip</title>
		<link>http://marlongrech.wordpress.com/2009/01/31/using-the-wpf-web-browser-control-in-a-tooltip/</link>
		<comments>http://marlongrech.wordpress.com/2009/01/31/using-the-wpf-web-browser-control-in-a-tooltip/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 09:51:47 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2009/01/31/using-the-wpf-web-browser-control-in-a-tooltip/</guid>
		<description><![CDATA[While writing a small app that I will be publishing here, I stumbled upon a strange issue. I was trying to put the WPF WebBrowser control inside a Tooltip yet the WebBrowser was not appearing on the screen. After an hour of debugging and trying out different things I found this WPF Forum thread that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=256&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>While writing a small app that I will be publishing here, I stumbled upon a strange issue. I was trying to put the WPF WebBrowser control inside a Tooltip yet the WebBrowser was not appearing on the screen. After an hour of debugging and trying out different things I found <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/63dd0da4-c955-4bda-9d2c-3bec2086f951/" target="_blank">this WPF Forum thread</a> that solved my issue… The problem has something to do with the WebBrowser being a native control and not a pure WPF control (so you are actually using an HwndHost in order to show it in a WPF app).  Also you might want to have a look at <a href="http://shevaspace.blogspot.com/2008/09/how-to-display-hwndhost-inside.html" target="_blank">this blog post</a>, it&#8217;s from the answerer of the forum post. Anyway, I just thought to share this with who ever is encountering this problem…..</p>
<p>Now I’m headed back coding my new project… should be cool, will post it shortly hopefully <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/256/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/256/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/256/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/256/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/256/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/256/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/256/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/256/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/256/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/256/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=256&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2009/01/31/using-the-wpf-web-browser-control-in-a-tooltip/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Client Profile + WPF = Superb Apps</title>
		<link>http://marlongrech.wordpress.com/2008/09/24/net-client-profile-wpf-superb-apps/</link>
		<comments>http://marlongrech.wordpress.com/2008/09/24/net-client-profile-wpf-superb-apps/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 21:19:09 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[.Net Client Profile]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2008/09/24/net-client-profile-wpf-superb-apps/</guid>
		<description><![CDATA[For those of you that have been asking where is Marlon? &#8220;Why did he stop blogging for these past 3 weeks?&#8221; Well I&#8217;ve been really really busy packaging 2 new hot products. I currently work at Uniblue Systems where we develop system utilities that help you keep you PC healthy and clean. I have been [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=209&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>For those of you that have been asking where is Marlon? &#8220;Why did he stop blogging for these past 3 weeks?&#8221; <br />Well I&#8217;ve been really really busy packaging 2 new hot products. I currently work at <a target="_blank" href="http://www.liutilities.com/">Uniblue Systems</a> where we develop system utilities that help you keep you PC healthy and clean. I have been working at Uniblue for 2 years but never in these 2 years I was excited as I am today. We released our <a target="_blank" href="http://www.liutilities.com/products/speedupmypc/">first 2 products</a> using WPF, packaged with .NET Framework Client Profile. </p>
<p><a target="_blank" href="http://blogs.windowsclient.net/trickster92/archive/2008/05/21/introducing-the-net-framework-client-profile.aspx">.NET Framework Client Profile</a> is really superb, it enables us at Uniblue to distribute our products to the end user in a fast and easy way. Before .NET Client Profile, it was very hard for companies such as Uniblue to ship software with .NET Framework because the download of the framework alone is massive. Our customers hate to wait ( don&#8217;t yours? ). With .NET Client Profile the download + installation is really fast and that&#8217;s why our customers love it! </p>
<p>Client Profile not only installs the basic .NET Framework components, but it installs .NET 3.5 SP1 (Client Only) which means you get all the WPF goodness + all new super stuff that .NET 3.5 SP1 provides. So yea we can also go crazy with user interfaces!&nbsp; If you want to check out one of our new products (developed fully with WPF) <a target="_blank" href="http://www.liutilities.com/products/speedupmypc/">check it out here</a> &#8230;. please submit as many feedback as possible&#8230; I can&#8217;t wait to see what you think &#8230;. </p>
<p>&nbsp;</p>
<p> <a href="http://marlongrech.files.wordpress.com/2008/09/sump.jpg"><img style="border-width:0;" border="0" alt="SUMP" src="http://marlongrech.files.wordpress.com/2008/09/sump-thumb.jpg?w=499&#038;h=366" width="499" height="366"></a>&nbsp;</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/209/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=209&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/09/24/net-client-profile-wpf-superb-apps/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/09/sump-thumb.jpg" medium="image">
			<media:title type="html">SUMP</media:title>
		</media:content>
	</item>
		<item>
		<title>Visual Studio 2008 + .Net 3.5 SP1 is out now</title>
		<link>http://marlongrech.wordpress.com/2008/08/11/visual-studio-2008-net-35-sp1-is-out-now/</link>
		<comments>http://marlongrech.wordpress.com/2008/08/11/visual-studio-2008-net-35-sp1-is-out-now/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 19:56:34 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2008/08/11/visual-studio-2008-net-35-sp1-is-out-now/</guid>
		<description><![CDATA[I&#8217;ve been counting the seconds for this to happen&#8230; It is finally here, Visual Studio 2008 SP1 and .Net 3.5 SP1 is out now. Get it from here.
Before installing this I would suggest that you have a look at this post. Quote from here &#8220;When installing Visual Studio 2008 Sp1 Beta1, you may encounter an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=194&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve been counting the seconds for this to happen&#8230; It is finally here, Visual Studio 2008 SP1 and .Net 3.5 SP1 is out now. <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=fbee1648-7106-44a7-9649-6d9f6d58056e&amp;DisplayLang=en">Get it from here</a>.</p>
<p>Before installing this I would suggest that you have a look at <a href="http://code.msdn.microsoft.com/RemoveKB944899/Release/ProjectReleases.aspx?ReleaseId=1030">this post</a>. <a href="http://code.msdn.microsoft.com/RemoveKB944899/Release/ProjectReleases.aspx?ReleaseId=1030">Quote from here</a> &#8220;When installing Visual Studio 2008 Sp1 Beta1, you may encounter an error that you must first remove KB944899 before setup can continue. This message can be displayed if you have installed any KB for Visual Studio 2008, not just KB944899.&#8221;</p>
<p>There could be some issue if you had the SP1 Beta installed so better to have a look instead of crying like a baby after <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a href="http://download.microsoft.com/download/A/2/8/A2807F78-C861-4B66-9B31-9205C3F22252/VS2008SP1Readme.htm#Installing">Do read the Read me file</a>!!!!! It is very important&#8230; I had a problem with the following but thanks to the readme file I knew what to do</p>
<h4>2.1.9 Windows Automatic Update notification appears during Visual Studio 2008 SP1 installation on Windows Vista</h4>
<p>Windows Automatic Update notification to restart the computer may appear when Visual Studio 2008 SP1 is being installed on a Windows Vista computer that does not have .NET Framework 2.0 SP2 and .NET Framework 3.0 SP2 installed. Allowing Windows Update to restart immediately will cause Visual Studio Setup to fail.</p>
<p><span style="text-decoration:underline;">To resolve this issue:</span></p>
<p>Postpone the restart until Visual Studio SP1 installation is finished.</p>
<p>&nbsp;</p>
<p>And yes it does take a long time, so be patient&#8230; it&#8217;s worth every second <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a href="http://marlongrech.files.wordpress.com/2008/08/success.jpg"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="228" alt="success" src="http://marlongrech.files.wordpress.com/2008/08/success-thumb.jpg?w=244&#038;h=228" width="244" border="0"></a> </p>
<p>&nbsp;</p>
<p>Have a nice evening with the brand new SP1&#8230; I love you Microsoft&#8230;. I LOVE YOU</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/194/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/194/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/194/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=194&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/08/11/visual-studio-2008-net-35-sp1-is-out-now/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/08/success-thumb.jpg" medium="image">
			<media:title type="html">success</media:title>
		</media:content>
	</item>
		<item>
		<title>My wish came true&#8230;. I can now use DataBinding in a ConverterParameter</title>
		<link>http://marlongrech.wordpress.com/2008/08/03/my-wish-came-true-i-can-now-use-databinding-in-a-converterparameter/</link>
		<comments>http://marlongrech.wordpress.com/2008/08/03/my-wish-came-true-i-can-now-use-databinding-in-a-converterparameter/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 13:06:38 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.0]]></category>
		<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2008/08/03/my-wish-came-true-i-can-now-use-databinding-in-a-converterparameter/</guid>
		<description><![CDATA[WARNING: This solution uses reflection so it cannot be used in XBAP or applications running with Partial Trust.
One of the things I hear often in forums and also lately on the WPF Disciples mailing list is, &#8220;How come I cannot use binding in ConverterParameters?&#8221; or &#8220;My personal pet peeve is the ConverterParameter. Ah if I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=191&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>WARNING: This solution uses reflection so it cannot be used in XBAP or applications running with Partial Trust.</p>
<p>One of the things I hear often in forums and also lately on the <a href="http://groups.google.com/group/wpf-disciples">WPF Disciples</a> mailing list is, &#8220;How come I cannot use binding in ConverterParameters?&#8221; or &#8220;My personal pet peeve is the ConverterParameter. Ah if I could only bind it to something in my ViewModel!!&#8221;</p>
<p>So let&#8217;s start by seeing why you cannot use binding for a ConverterParameter. The simple answer to that would be, a ConverterParameter is not a DependencyProperty thus you cannot use Binding. Yet the following question would be, WHY isn&#8217;t it a DependencyProperty?? Well there are a few things we must have a look at here. First of all the Binding class is not a DependencyObject. Secondly the BindingBase class seals itself when the binding activates, this means that once the Binding is activated you cannot change any properties. If you try to do so you get a nice InvalidOperationException saying &#8220;Binding cannot be changed after it has been used&#8221;.</p>
<p>Yet, the WPF platform is very flexible so I decided to start digging into it and see what I can come up with (obviously always consulting the <a href="http://www.drwpf.com/">WPF KING OF KINGS, Dr.WPF)</a>.</p>
<h3>The first idea&#8230;</h3>
<p>My first idea was to create a MarkupExtension that spits out an instance of an object that has a Dependency Property that you can bind with (some sort of Proxy that can update the Binding and specify a new value). So your XAML would look like this</p>
<p>{Binding ElementName=checkbox1, Path=IsChecked, Converter={StaticResource conv}, ConverterParameter={code:BindableParameter {Binding ElementName=checkbox2, Path=IsChecked}} }</p>
<p>Yet this failed miserably because of a simple yet hard problem in the design I had. You cannot get the BindingExpression of that binding because the BindingExpression is not constructed yet. I could get the Binding instance but the Binding instance alone is nothing without the BindingExpression since you can update a binding by calling the UpdateSource method of the Binding expression.</p>
<h3>Back to the drawing boards&#8230;. and finally the Solution</h3>
<p>So let&#8217;s have a look at what we want to achieve. We want to</p>
<p>1. Let the User Specify a Binding for a ConverterParameter somewhere in the XAML<br />
2. Make sure that when a value of the ConverterParameter changes the original Binding gets updated.</p>
<p>In order to solve problem 1, I decided to go to my best friend, AttachedProperties. Basically the idea is that you set an attached property that specifies the value of a ConverterParameter of a specific binding. So the XAML would look something like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;ToggleButton Content=<span style="color:#006080;">"I am bound to the checkbox"</span> x:Name=<span style="color:#006080;">"toggle"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>      code:BindableParameter.BindParameter=<span style="color:#006080;">"{code:BindableParameter ToggleButton.IsChecked, Binding={Binding ElementName=checkbox2, Path=IsChecked}}"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>      IsChecked=<span style="color:#006080;">"{Binding Converter={StaticResource conv}, ElementName=checkbox1, Path=IsChecked}"</span>/&gt;</pre>
</div>
</div>
<p>In the attached property you specify what Binding you want to target by supplying the DependencyProperty to which the original Binding is applied. In this case the original Binding is for the IsChecked property of the ToggleButton. Then you can simple specify the Binding you want for the ConverterParameter by setting the Binding property of the BindableParameter markup extension.</p>
<p>Now that you have supplied this information, the BindableParameter can do some tricks for you. Basically once you specify the BindParameter attached property the BindableParameter will get a BindingExpression for the original binding and make sure to set the ConverterParameter when ever needed. Have a look at the PropertyChanged event handler of the BindParameter Attached property.</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> OnBindParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     FrameworkElement element = d <span style="color:#0000ff;">as</span> FrameworkElement;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">if</span> (element == <span style="color:#0000ff;">null</span>)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> InvalidOperationException(<span style="color:#006080;">"BindableParameter can be applied to a FrameworkElement only"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     BindableParameter parameter = (BindableParameter)e.NewValue;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     element.Initialized += <span style="color:#0000ff;">delegate</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         parameter.TargetExpression = BindingOperations.GetBindingExpression(element, parameter.TargetProperty);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         parameter.TargetBinding = BindingOperations.GetBinding(element, parameter.TargetProperty);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         <span style="color:#008000;">//update the converter parameter </span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>         InvalidateBinding(parameter);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     };</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> }</pre>
</div>
</div>
<p>As you can see, in the above code, all we are doing is get the instance of the BindingExpression and storing it in an instance variable(TargetExpression). Same goes for the Binding, storing it in the TargetBinding. Now that we have all this information we can do Step 2 (Make sure that when a value of the ConverterParameter changes the original Binding gets updated)</p>
<p>In the Property Changed event handler of the Binding we must force the BindingExpression to update and change the value of the converter parameter. This can be done quite easily one would say but there are 2 main problems.</p>
<p>Problem 1. How to make the binding work for the BindableParamater?? The BindableParameter is not in the LogicalTree because it is just a DependencyObject thus the Binding that you set will not be valid. In order to overcome this issue we can do a little trick with the Freezable class. If we make the BindableParameter inherit from the Freezable it gets the inheritance context thus Binding is now valid. <a href="http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=36">For more info on this technique visit Dr. WPF blog</a></p>
<p>Problem 2. How can we change the value of ConverterParameter if the Binding is sealed (remember? the Binding class will throw an InvalidOperationException if we try to change a property of the Binding). This problem required me to do some hard core disassembling of the Binding class and I found out that each Property Setter of the Binding class has a call to a method CheckSealed. This method looks like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">void</span> CheckSealed()</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>._isSealed)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> InvalidOperationException(SR.Get(<span style="color:#006080;">"ChangeSealedBinding"</span>));</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span></pre>
</div>
</div>
<p>So what we must do is to get the _isSealed Field info by reflection and change it&#8217;s value to false, then change the ConverterParameter and finally put the _isSealed value back to true so that WPF does not notice us playing tricks on him <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Once we do that we are free to change the value of the ConverterParameter and we can Refresh the Binding</p>
<p>Something like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> InvalidateBinding(BindableParameter param)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (param.TargetBinding != <span style="color:#0000ff;">null</span> &amp;&amp; param.TargetExpression != <span style="color:#0000ff;">null</span>)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#008000;">//this is a hack to trick the WPF platform in thining that the binding is not sealed yet and then change the value of the converter parameter</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         <span style="color:#0000ff;">bool</span> isSealed = (<span style="color:#0000ff;">bool</span>)isSealedFieldInfo.GetValue(param.TargetBinding);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         <span style="color:#0000ff;">if</span> (isSealed)<span style="color:#008000;">//change the is sealed value</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             isSealedFieldInfo.SetValue(param.TargetBinding, <span style="color:#0000ff;">false</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         param.TargetBinding.ConverterParameter = param.ConverterParameterValue;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         <span style="color:#0000ff;">if</span> (isSealed)<span style="color:#008000;">//put the is sealed value back as it was...</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>             isSealedFieldInfo.SetValue(param.TargetBinding, <span style="color:#0000ff;">true</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>         <span style="color:#008000;">//force an update to the binding</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>         param.TargetExpression.UpdateTarget();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span> }</pre>
</div>
</div>
<p>And there it is &#8230;. I can now use binding for a ConverterParameter and the XAML looks like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;Window.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>    &lt;code:DummyConverter x:Key=<span style="color:#006080;">"conv"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> &lt;/Window.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>    &lt;CheckBox x:Name=<span style="color:#006080;">"checkbox2"</span> Content=<span style="color:#006080;">"I am the parameter for the converter"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>    &lt;CheckBox x:Name=<span style="color:#006080;">"checkbox1"</span> Content=<span style="color:#006080;">"I am bound directly"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>    &lt;ToggleButton Content=<span style="color:#006080;">"I am bound to the checkbox"</span> x:Name=<span style="color:#006080;">"toggle"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>                  code:BindableParameter.BindParameter=<span style="color:#006080;">"{code:BindableParameter ToggleButton.IsChecked, Binding={Binding ElementName=checkbox2, Path=IsChecked}}"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>                  IsChecked=<span style="color:#006080;">"{Binding Converter={StaticResource conv}, ElementName=checkbox1, Path=IsChecked}"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> &lt;/StackPanel&gt;</pre>
</div>
</div>
<p>Hope you enjoy this &#8230;..</p>
<p><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/BinableConverterParameters.rar">DOWNLOAD SOURCE CODE</a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f03%2fmy-wish-came-true-i-can-now-use-databinding-in-a-converterparameter%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f03%2fmy-wish-came-true-i-can-now-use-databinding-in-a-converterparameter%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/191/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/191/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/191/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/191/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/191/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=191&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/08/03/my-wish-came-true-i-can-now-use-databinding-in-a-converterparameter/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f03%2fmy-wish-came-true-i-can-now-use-databinding-in-a-converterparameter%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>NameScope, my name is Marlon you know&#8230;.</title>
		<link>http://marlongrech.wordpress.com/2008/08/02/namescope-my-name-is-marlon-you-know/</link>
		<comments>http://marlongrech.wordpress.com/2008/08/02/namescope-my-name-is-marlon-you-know/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 09:26:00 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.0]]></category>
		<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2008/08/02/namescope-my-name-is-marlon-you-know/</guid>
		<description><![CDATA[Introduction
Did you ever wonder why you can have the same name registered for different parts of your XAML? Here is an example (notice the name &#8220;border&#8221; in different parts of the XAML)


   1: &#60;ContentControl Content="Hello world" x:Name="border"&#62;
   2:     &#60;ContentControl.ContentTemplate&#62;
   3:      [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=189&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3>Introduction</h3>
<p>Did you ever wonder why you can have the same name registered for different parts of your XAML? Here is an example (notice the name &#8220;border&#8221; in different parts of the XAML)</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> &lt;ContentControl Content=<span style="color:#006080;">"Hello world"</span> x:Name=<span style="color:red;"><strong>"border"</strong></span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span>     &lt;ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span>         &lt;DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span>             &lt;Border BorderBrush=<span style="color:#006080;">"Silver"</span> BorderThickness=<span style="color:#006080;">"1"</span> CornerRadius=<span style="color:#006080;">"5"</span> Margin=<span style="color:#006080;">"20"</span> x:Name=<span style="color:red;"><strong>"border"</strong></span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span>                 &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   6:</span>                     &lt;TextBlock Text=<span style="color:#006080;">"I am a DataTemplate"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   7:</span>                     &lt;ContentPresenter Content=<span style="color:#006080;">"{Binding}"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span> VerticalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   8:</span>                 &lt;/StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   9:</span>             &lt;/Border&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  10:</span>         &lt;/DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  11:</span>     &lt;/ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  13:</span>     &lt;ContentControl.Template&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  14:</span>         &lt;ControlTemplate TargetType=<span style="color:#006080;">"ContentControl"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  15:</span>             &lt;Border BorderBrush=<span style="color:#006080;">"Silver"</span> BorderThickness=<span style="color:#006080;">"1"</span> CornerRadius=<span style="color:#006080;">"5"</span> x:Name=<span style="color:red;"><strong>"border"</strong></span> &gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  16:</span>                 &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  17:</span>                     &lt;TextBlock Text=<span style="color:#006080;">"I am a ControlTemplate"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  18:</span>                     &lt;ContentPresenter /&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  19:</span>                 &lt;/StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  20:</span>             &lt;/Border&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  21:</span>         &lt;/ControlTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  22:</span>     &lt;/ContentControl.Template&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  23:</span> &lt;/ContentControl&gt;</pre>
</div>
</div>
<p>As you can see the XAML above <strong>&#8220;<span style="color:#ff0000;">border</span>&#8220;</strong> is registered 3 times. This is possible because of WPF <a href="http://msdn.microsoft.com/en-us/library/ms746659.aspx">Namescopes</a>. Basically the Window(that I am using to put the ContentControl in) has it&#8217;s own NameScope the DataTemplate also has it&#8217;s own and also the ControlTemplate (some actually implement the <a href="http://msdn.microsoft.com/en-us/library/system.windows.markup.inamescope(VS.85).aspx">INameScope</a> interface themselves).</p>
<p>Namescopes are a very important concept that one must understand. Why? Let me give you an example.</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span>     &lt;ContentControl Content=<span style="color:#006080;">"Hello world"</span> x:Name=<span style="color:#006080;">"border"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span>         &lt;ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span>             &lt;DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span>                 &lt;Border BorderBrush=<span style="color:#006080;">"Silver"</span> BorderThickness=<span style="color:#006080;">"1"</span> CornerRadius=<span style="color:#006080;">"5"</span> Margin=<span style="color:#006080;">"20"</span> x:Name=<span style="color:#006080;">"border"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   6:</span>                     &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   7:</span>                         &lt;TextBlock Text=<span style="color:#006080;">"I am a DataTemplate"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   8:</span>                     &lt;ContentPresenter Content=<span style="color:#006080;">"{Binding}"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span> VerticalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   9:</span>                     &lt;CheckBox x:Name=<span style="color:red;"><strong>"checkbox"</strong></span> Content=<span style="color:#006080;">"Checkbox here"</span> /&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  10:</span>                 &lt;/StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  11:</span>                 &lt;/Border&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  12:</span>             &lt;/DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  13:</span>         &lt;/ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  14:</span>     &lt;/ContentControl&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  15:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  16:</span>     &lt;ToggleButton Content=<span style="color:#006080;">"Toggle button bound to checkbox"</span> IsChecked=<span style="color:red;"><strong>"{Binding ElementName=checkbox, Path=IsChecked}"</strong></span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  17:</span> &lt;/StackPanel&gt;</pre>
</div>
</div>
<p>The XAML above has a ContentControl that has a ContentTemplate with a CheckBox and a Toggle button that is trying to bind to that checkbox. The binding is using ElementName to try to find the checkbox yet that will <strong>NOT</strong> be successful because the &#8220;checkbox&#8221; is registered in a different NameScope (remember a DataTemplate has it&#8217;s own NameScope!)</p>
<h3></h3>
<h3>Namescope and Animations</h3>
<p>Another scenario where NameScopes are heavily used are animation. When you create a Storyboard you specify a Storyboard.TargetName attached property to specify which element you want to animate. Something like this&#8230;.</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> &lt;Window.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span>     &lt;Storyboard x:Key=<span style="color:#006080;">"animation"</span> &gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span>         &lt;DoubleAnimation To=<span style="color:#006080;">"50"</span> AutoReverse=<span style="color:#006080;">"True"</span> RepeatBehavior=<span style="color:#006080;">"Forever"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span>                        <span style="color:red;"><strong>Storyboard.TargetName=<span style="color:red;">"button"</span> </strong></span> Storyboard.TargetProperty=<span style="color:#006080;">"Height"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span>     &lt;/Storyboard&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   6:</span> &lt;/Window.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   7:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   8:</span> &lt;Window.Triggers&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   9:</span>     &lt;EventTrigger RoutedEvent=<span style="color:#006080;">"FrameworkElement.Loaded"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  10:</span>         &lt;BeginStoryboard Storyboard=<span style="color:#006080;">"{StaticResource animation}"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  11:</span>     &lt;/EventTrigger&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  12:</span> &lt;/Window.Triggers&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  13:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  14:</span> &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  15:</span>     &lt;ContentControl Content=<span style="color:#006080;">"Hello world"</span> x:Name=<span style="color:#006080;">"border"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  16:</span>         &lt;ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  17:</span>             &lt;DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  18:</span>                 &lt;Border BorderBrush=<span style="color:#006080;">"Silver"</span> BorderThickness=<span style="color:#006080;">"1"</span> CornerRadius=<span style="color:#006080;">"5"</span> Margin=<span style="color:#006080;">"20"</span> x:Name=<span style="color:#006080;">"border"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  19:</span>                     &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  20:</span>                         &lt;TextBlock Text=<span style="color:#006080;">"I am a DataTemplate"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  21:</span>                     &lt;ContentPresenter Content=<span style="color:#006080;">"{Binding}"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span> VerticalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  22:</span>                     &lt;CheckBox x:Name=<span style="color:#006080;">"checkbox"</span> Content=<span style="color:#006080;">"Checkbox here"</span> /&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  23:</span>                 &lt;/StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  24:</span>                 &lt;/Border&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  25:</span>             &lt;/DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  26:</span>         &lt;/ContentControl.ContentTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  27:</span>     &lt;/ContentControl&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  28:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  29:</span>     &lt;ToggleButton Content=<span style="color:#006080;">"Toggle button bound to checkbox"</span> Height=<span style="color:#006080;">"25"</span> x:Name=<span style="color:red;"><strong>"button"</strong></span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  30:</span> &lt;/StackPanel&gt;</pre>
</div>
</div>
<p>The above code would execute as expected, the ToggleButton named &#8220;button&#8221; would start animating it&#8217;s height when the window is Loaded. But what if you want to animate something in the DataTemplate? If you try to create a Storyboard in the Resources section of the window to animate the Checkbox  named &#8220;checkbox&#8221; you would get an <strong>InvalidOperationException</strong> saying <strong><em>&#8216;checkbox&#8217; name cannot be found in the name scope of &#8216;NamescopeExample.Window1&#8242;. </em></strong>This is because the checkbox lives in a different NameScope from the storyboard. One thing you can do is to move the Storyboard in the DataTemplate.Resources so that the storyboard would be created in the same Namescope of the checkbox. Something like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:1000px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> &lt;DataTemplate&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span>     &lt;DataTemplate.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span>         &lt;Storyboard x:Key=<span style="color:#006080;">"checkboxAnimation"</span> RepeatBehavior=<span style="color:#006080;">"Forever"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span>             &lt;ColorAnimation To=<span style="color:#006080;">"Red"</span> AutoReverse=<span style="color:#006080;">"True"</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span>                 Storyboard.TargetName=<span style="color:#006080;">"checkbox"</span> Storyboard.TargetProperty=<span style="color:#006080;">"(Control.Background).(SolidColorBrush.Color)"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   6:</span>         &lt;/Storyboard&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   7:</span>     &lt;/DataTemplate.Resources&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   8:</span>     &lt;Border BorderBrush=<span style="color:#006080;">"Silver"</span> BorderThickness=<span style="color:#006080;">"1"</span> CornerRadius=<span style="color:#006080;">"5"</span> Margin=<span style="color:#006080;">"20"</span> x:Name=<span style="color:#006080;">"border"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   9:</span>         &lt;StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  10:</span>             &lt;TextBlock Text=<span style="color:#006080;">"I am a DataTemplate"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  11:</span>         &lt;ContentPresenter Content=<span style="color:#006080;">"{Binding}"</span> HorizontalAlignment=<span style="color:#006080;">"Center"</span> VerticalAlignment=<span style="color:#006080;">"Center"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  12:</span>         &lt;CheckBox x:Name=<span style="color:#006080;">"checkbox"</span> Content=<span style="color:#006080;">"Checkbox here"</span> Background=<span style="color:#006080;">"Black"</span> /&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  13:</span>     &lt;/StackPanel&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  14:</span>     &lt;/Border&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  15:</span>     &lt;DataTemplate.Triggers&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  16:</span>         &lt;EventTrigger RoutedEvent=<span style="color:#006080;">"FrameworkElement.Loaded"</span>&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  17:</span>             &lt;BeginStoryboard Storyboard=<span style="color:#006080;">"{StaticResource checkboxAnimation}"</span>/&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  18:</span>         &lt;/EventTrigger&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  19:</span>     &lt;/DataTemplate.Triggers&gt;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">  20:</span> &lt;/DataTemplate&gt;</pre>
</div>
</div>
<h3>Namescope and C# code</h3>
<p>But what if you are creating elements in code&#8230; One would say out &#8220;Easy&#8230; Just set the name property&#8221; like so&#8230;.</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> ToggleButton button = <span style="color:#0000ff;">new</span> ToggleButton();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span> button.Height = 25;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span> button.Content = <span style="color:#006080;">"Toggle Button"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span> button.Name = <span style="color:#006080;">"button"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span> container.Children.Add(button);</pre>
</div>
</div>
<p>No no no and NO&#8230;. This does not work! Setting the Name property of the control will not register that Control to the NameScope. So how to do it? Easy.. have a look</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> ToggleButton button = <span style="color:#0000ff;">new</span> ToggleButton();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span> button.Height = 25;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span> button.Content = <span style="color:#006080;">"Toggle Button"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span> NameScope.GetNameScope(<span style="color:#0000ff;">this</span>).RegisterName(<span style="color:#006080;">"button"</span>, button);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span> container.Children.Add(button);</pre>
</div>
</div>
<p>All you have to do is, first find in which NameScope you want to register the element (in this case I want to add it to the Window NameScope), secondly register the element to that NameScope by using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.registername(VS.85).aspx">RegisterName</a> method of the INameScope interface.</p>
<p>You can even use the RegisterName method of the FrameworkElement. For example if I want to add a Button to a StackPanel, you can ask the StackPanel to register the name of the Button. In my case I could have done like this</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   1:</span> ToggleButton button = <span style="color:#0000ff;">new</span> ToggleButton();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   2:</span> button.Height = 25;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   3:</span> button.Content = <span style="color:#006080;">"Toggle Button"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   4:</span> container.RegisterName(<span style="color:#006080;">"button"</span>, button);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;"><span style="color:#606060;">   5:</span> container.Children.Add(button);</pre>
</div>
</div>
<h3></h3>
<h3>Some other tips and related articles&#8230;</h3>
<p>If you have an element in the Resources section you will not be allowed to set a name for that element. One way of enabling that element with a name is by using the RegisterName method of the NameScope class as explained above (you might want to set the name for animations for example).</p>
<p>You might want to also implement your own NameScope by implementing the INameScope interface. I saw a <a href="http://agsmith.wordpress.com/2008/07/17/elementname-binding-in-tooltips-borrowing-a-namescope/">very clever solution by doing such a thing</a>, by WPF super hero Andrew Smith.</p>
<p><a href="http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/">Josh Smith the WPF Rock star and his Element Spy</a> is also a brilliant article that must be read..</p>
<p>
<a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f02%2fnamescope-my-name-is-marlon-you-know%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f02%2fnamescope-my-name-is-marlon-you-know%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/189/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/189/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/189/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=189&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/08/02/namescope-my-name-is-marlon-you-know/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f08%2f02%2fnamescope-my-name-is-marlon-you-know%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>