<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Colin Eberhardt&#039;s Adventures in .NET &#187; WPF</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 10:21:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Windows 8 &#8211; An OS of two halves.</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 20:59:26 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows 8]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1662</guid>
		<description><![CDATA[In this blog post I take a look at Windows 8 on the outside, from a user perspective; and on the inside, from a developer perspective to see how it will change the way we develop with Windows on tablets, smartphones and the desktop when it is released next year. In Anaheim, California, the Microsoft [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F09%252Fwindows-8-an-os-of-two-halves%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Windows%208%20-%20An%20OS%20of%20two%20halves.%20%23%22%20%7D);"></div>
<p><em>In this blog post I take a look at Windows 8 on the outside, from a user perspective; and on the inside, from a developer perspective to see how it will change the way we develop with Windows on tablets, smartphones and the desktop when it is released next year.</em></p>
<p>In Anaheim, California, the Microsoft //build/ conference is drawing to a close. And as the attendees head for home, with their shiny new Samsung tablets under their arms, the news of Windows 8 and the impact it has on end-users and developers is starting to sink in around the globe.</p>
<p>There is no doubt that the impact of Windows 8 is going to be very large, it heralds the biggest change in the Windows architecture since 1993 and from an end-user perspective it is like no other version of Windows before it.</p>
<p>So why such a radical change to both the UI and the architecture? and why now? The answer to this question is quite simple &#8230; <strong>tablets</strong>. Previous versions of Windows have been targeted primarily at the desktop, laptop and netbook form-factors where the primary input devices are mouse and keyboard, and memory, disk-space, CPU performance is aplenty. Tablets have made the odd cameo appearance in the history of computing, but it is only recently with the launch of the iPad that they have suddenly become mainstream.</p>
<p>To take the Windows OS to a tablet it needs to be stripped-down, made faster and support multi-touch (or touch-first &#8211; a new Microsoft buzzword) and with Windows 8 Microsoft have done just that. With the requirements of a tablet interface being so different to a desktop, how have Microsoft reconciled these differences? The answer is a little surprising &#8230; <em> they haven&#8217;t</em>! They could have launched a dedicated Tablet Windows OS, just as they have done on the mobile with Windows Phone 7, but instead they have launched a single OS that works for both tablet and desktop. As a result it has something of a split personality. You can choose to run either the Metro or the &#8216;classic&#8217; desktop UI on your tablet computer and you have exactly the same choice on your desktop.</p>
<h2>An end-user perspective</h2>
<p>The Windows 8 user interface uses the same Metro style that Microsoft introduced with Windows Phone 7, bright colours, clean edges, a lack of drop-shadows and other adornments. The style is heavily influenced by fonts and designs used for road and street-signs. The same concepts of clarity-at-a-distance translates very well to a small smartphone screen and should be equally at home on the tablet.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/windows8.jpg" alt="" title="windows8" width="560" height="315" class="aligncenter size-full wp-image-1663" /></p>
<p>When applications are launched they run full-screen, with a similar multi-tasking model to Windows Phone 7 where  hidden applications are suspended. These features, combined with the fast shutdown / restart times make sense for tablet users where limiting the work that the processor does will significantly enhance battery life.</p>
<p>You can run multiple applications side-by-side in a split-screen mode:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/windows-8-split-screen.jpg" alt="" title="windows-8-split-screen" width="560" height="335" class="aligncenter size-full wp-image-1664" /></p>
<p>The current preview of Windows 8 has a few of the familiar applications available in the new Metro-desktop, these include IE which is now chromeless, i.e. it is permanently in full-screen mode. Gestures are used to reveal the open tabs or URL. Notably the Metro IE does not support any plugins, i.e. Flash, Silverlight &#8211; more on this later!</p>
<p>Metro applications for Windows 8 are distributed via the Windows Marketplace in much the same way as they are for Windows Phone (and iPad, Android etc&#8230;). </p>
<p>The Windows 8 UI clearly has tablets in mind and recent reviews by <a href="http://www.engadget.com/2011/09/13/windows-8-for-tablets-hands-on-preview/">Engadget</a> and <a href="http://gizmodo.com/5839665/windows-8-slate-hands-on-its-fantastic-but-dont-sell-your-ipad">Gizmodo</a> seem to indicate that the experience is very good and agree that when released it could be a serious competitor to the iPad.</p>
<p>However, hidden behind the glossy Windows 8 UI sits the more traditional Windows 7 style desktop. Simply click (or touch) the desktop button and the old-familiar Windows desktop appears. It almost feels like the desktop is an app running within the new Metro UI &#8211; you can also run a split screen which combines the Metro and desktop UIs:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/MetroDesktop.jpg" alt="" title="MetroDesktop" width="560" height="315" class="aligncenter size-full wp-image-1666" /></p>
<p>Interestingly if you <em>want </em>to run IE with plugins, there is a fully-featured version of IE present on the desktop. Try explaining these differences to your grandmother!</p>
<p>For a tablet, Windows 8 gives you the best of both worlds, an immersive and tactile Metro interface, plus the old-familiar Windows desktop.</p>
<p>On the desktop, Windows 8 does feel a bit strange. Metro applications are designed to work with the mouse and the keyboard, so it functions just fine, however, on a desktop monitor the Metro UI feels like a bit large and a touch gimmicky. The Metro concepts work well for small screens, but in my opinion are less effective on a large desktop monitor, especially in the absence of touch. I doubt it will make a big impact for desktop users, but at least they have the choice! When used on a PC with multiple monitors the new Metro UI is confined to a single screen, again further illustrating its tablet focus.</p>
<h2>A developer perspective</h2>
<p>Windows 8 marks a radical change from the outside, but what about on the inside? What is the developer story?</p>
<p>One of the most important diagrams revealed in the keynote is the Windows 8 architecture diagram:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/Windows8Architecture.jpg" alt="" title="Windows8Architecture" width="550" height="309" class="aligncenter size-full wp-image-1667" /></p>
<p>It is clear that the split personality of Windows 8 runs very deep. In this diagram you can see that Metro style apps and desktop apps share the kernel, but nothing else. This is a pretty significant change in Windows architecture; Metro apps are finally free of the Win32 APIs which .NET has been built upon.</p>
<p>The APIs for developing Metro apps reside within the WinRT. A few keys things to know about this new architecture are:</p>
<ul>
<li>WinRT applications are chromless, fullscreen, with no pop-up or modal dialogs</li>
<li>WinRT is an Object Oriented replacement for Win32</li>
<li>WinRT is very similar to .NET, however a number of APIs such as file I/O, security etc&#8230; are not present.</li>
<li>You can develop with C++, or C# etc&#8230; in much the same way as .NET applications</li>
<li>The UI defined using XAML, as per WPF and Silverlight</li>
<li>You can potentially port Silverlight applications to WinRT by changing a few namespaces</li>
<li>JavaScript is a first-class citizen of Windows 8, using a HTML UI</li>
</ul>
<p>The above already tells us a lot about WinRT and Metro applications. My first thoughts are that they are much more similar to Windows Phone 7 applications than desktop WPF applications. They live within a sandbox which uses a restricted subset of APIs, you certainly cannot write a virus checked or hard disk partitioning tool with WinRT!</p>
<p>Furthermore, when developing Metro UI apps, Visual Studio &#8216;deploys&#8217; your application too your Windows 8 Metro desktop in exactly the same way as it does with the WP7 phone.</p>
<p>So what about existing .NET applications? The important thing to know is that all the &#8216;old&#8217; APIs are still there. Your WPF, Silverlight and WinForms applications are safe! There were also a few new WPF features announced at //build/ as part of .NET4.5, but they are not very interesting, it is clear that Microsoft has been putting its energy into other things.</p>
<p>The JavaScript / HTML5 story is a bit of an odd one. Clearly Microsoft has embraced these technologies, with JavaScript having full access to the WinRT. Furthermore, they have added <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br211385">APIs for creating Metro UIs</a> with HTML. However, I am not sure why someone would want to use JavaScript to create Windows 8 Metro applications? The main strength of JavaScript and HTML5 is the portability due to its support on a wide range of browsers, tablets, phones, etc&#8230; The Windows 8 JavaScript Metro apps are &#8216;tied&#8217; to WinRT so are in no way portable.</p>
<h2>Questions &#8230;</h2>
<p>Lots of questions &#8230; the rather complicated nature of Windows 8 has resulted in quite a lot of confusion. </p>
<p><strong>Is Silverlight Dead?</strong> &#8211; this was probably the most oft asked question before //build/ and it is still being asked. The news that the<a href="http://blogs.msdn.com/b/b8/archive/2011/09/14/metro-style-browsing-and-plug-in-free-html5.aspx"> Metro IE10 will not run Silverlight</a> is certainly not good news for the technology, however, it does not mean it is dead &#8211; Silverlight is still present in the desktop IE10 browser (as is Flash). Recently Silverlight has been re-positioned as the technology of choice for more complex line-of-business applications, the types of application which sit better in the Windows 8 &#8216;classic&#8217; desktop. Furthermore, whilst the Metro UI is leading the push into tablet computing, I doubt it will take off on the desktop. So, no, Silverlight is not dead, but it has unfortunately missed out on being part of the Metro UI for tablets.</p>
<p>There is a relatively simple migration path for <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh465136%28v=VS.85%29.aspx">Silverlight and Windows Phone 7 applications to the new Metro WinRT</a>. This might work for full-screen Silverlight applications, but will not work for applications that mix HTML and Silverlight content within a browser.</p>
<p><strong>Is WPF Dead?</strong> &#8211; Again, for much the same reasons as Silerlight the answer is &#8220;no&#8221;. WPF is a framework intended for line-of-business applications, and those belong on the Windows 8 desktop.</p>
<p><strong>How does this impact Windows Phone 7?</strong> &#8211; it doesn&#8217;t. The next version of WP7 is going RTM shortly and there has been no change to these plans. In fact for WP7 developers Windows 8 is a great opportunity. The WinRT seems most closely related to Silverlight for WP7, so your skills and code are readily portable to the tablet.</p>
<p><strong>Should I write my app as a desktop or Metro app?</strong> &#8211; this is probably the single hardest question that the Windows 8 preview has raised. As application developers we now have two very different ways which we can deliver applications to the desktop and tablet, either the new Metro UI, powered by the (deliberately) limited WinRT; or desktop applications using WPF or Silverlight. I think it will be very interesting to see how Microsoft tackles this problem with its own applications. So far the Windows 8 preview has Metro UI based Twitter, Weather and Social Networking applications and a desktop based Visual Studio. There is also IE which is available in both desktop and Metro, albeit with quite different functionality. Where will Outlook, Word, Excel sit in the new Windows 8?</p>
<p>I think there are rather a lot of applications that will want to make use of both Metro and desktop UIs. Take Outlook for example, you could imagine a simplified Metro interface sitting on one of your monitors, but a more feature rich desktop version available on the other.</p>
<p>One other point worth considering is &#8216;reach&#8217;. It will take a long time before everyone is running a Windows 8 PC, so if you choose to deliver your application as a Metro app, you are restricting yourself just to the Windows 8 user-base. However, if you are targeting the Windows tablet, you can of course guarantee that they will run Windows 8.</p>
<h2>Conclusions</h2>
<p>Windows 8 presents a significant change in the way we develop Windows applications.  The split personality of the Windows 8 user interface and architecture is going to cause confusion on many levels.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/TwoHeaded.jpg" alt="" title="TwoHeaded" width="461" height="318" class="aligncenter size-full wp-image-1668" /></p>
<p>The technical implications of WinRT and how it relates to .NET are slowly starting to become clear, thanks to great blog posts like <a href="http://tirania.org/blog/archive/2011/Sep-15.html">&#8220;WinRT Demystified&#8221;</a>.</p>
<p>Personally what I think will take a little bit longer to understand is how we design applications for Windows 8, as we start to discover what works well in Metro, what should remain on the desktop and what we deliver to both.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Silverlight UK User Group Presentation on Cross Platform XAML</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 10:48:09 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1567</guid>
		<description><![CDATA[Earlier this week I presented a talk to the Silverlight UK User Group on cross-platform application development with WPF, Silverlight and Windows Phone 7. Thanks to Gergley Orosz for these pictures Thanks to all who attended my talk. Here is a copy of the presentation I gave: You can see the Windows Phone 7 XAML [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F07%252Fsilverlight-uk-user-group-presentation-on-cross-platform-xaml%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Silverlight%20UK%20User%20Group%20Presentation%20on%20Cross%20Platform%20XAML%20%23%22%20%7D);"></div>
<p>Earlier this week I presented a talk to the <a href="http://consultingblogs.emc.com/markmann/archive/2011/06/07/silverlight-uk-user-group-june-2011-agenda.aspx">Silverlight UK User Group</a> on cross-platform application development with WPF, Silverlight and Windows Phone 7.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/sluguk.jpg" alt="" title="sluguk" width="626" height="156" class="aligncenter size-full wp-image-1568" /></p>
<p><small>Thanks to <a href="http://twitter.com/#!/gergelyorosz">Gergley Orosz</a> for these pictures</small></p>
<p>Thanks to all who attended my talk. Here is a copy of the presentation I gave:</p>
<p><object id="__sse8477573" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=slugukpresentation-110701041841-phpapp02&#038;stripped_title=silverlight-uk-user-group-cross-platform-xaml-applications&#038;userName=colineberhardt" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse8477573" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=slugukpresentation-110701041841-phpapp02&#038;stripped_title=silverlight-uk-user-group-cross-platform-xaml-applications&#038;userName=colineberhardt" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></p>
<p>You can see the Windows Phone 7 XAML Finance application which I demonstrated in the following video:</p>
<p><iframe width="640" height="510" src="http://www.youtube.com/embed/ISWu2VOKIyc" frameborder="0" allowfullscreen></iframe></p>
<p>The Silverlight version is <a href="http://www.scottlogic.co.uk/blog/colin/xaml-finance/">also available online</a>.</p>
<p>Finally, the sourcecode for the simple cross-platform Twitter application I demonstrated is available to download: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/CrossPlatformDemo.zip'>CrossPlatformDemo.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automatically Showing ToolTips on a Trimmed TextBlock (Silverlight + WPF)</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 16:25:54 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1172</guid>
		<description><![CDATA[Both WPF and Silverlight have a property TextTrimming=”WordEllipsis”, which trims the text that a TextBlock displays based on the available width. This blog post describes a simple method for automatically showing the full text as a tooltip whenever the text is trimmed. This is presented as an attached behaviour and works in both WPF and [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F01%252Fautomatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Automatically%20Showing%20ToolTips%20on%20a%20Trimmed%20TextBlock%20%28Silverlight%20%2B%20WPF%29%20%23%22%20%7D);"></div>
<p><em>Both WPF and Silverlight have a property TextTrimming=”WordEllipsis”, which trims the text that a TextBlock displays based on the available width. This blog post describes a simple method for automatically showing the full text as a tooltip whenever the text is trimmed. This is presented as an attached behaviour and works in both WPF and Silverlight</em></p>
<p>A few weeks ago I blogged about a Silverlight solution for <a href="http://www.scottlogic.co.uk/blog/colin/2011/01/showing-tooltips-on-trimmed-textblock-silverlight/">automatically adding tooltips when a TextBlock Text is trimmed</a> and renders an ellipsis. I found a decent looking WPF solutions on the web and linked it in my article, however, based on the comments to my previous blog post, it looks like the WPF solution didn&#8217;t work too well, failing to respect font size etc&#8230; In this blog post I have updated my solution to be cross-platform, working on WPF <strong>and</strong> Silverlight.</p>
<p>To briefly recap, my solution for automatically adding tooltips to trimmed text relies on the slightly odd behaviour of the <code>TextBlock</code> where its <code>ActualWidth</code> is reported as the width of the text without trimming:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextTrimming.png" alt="" title="TextTrimming" width="568" height="257" class="aligncenter size-full wp-image-1076" /></p>
<p>In order to determine whether to show a tooltip, all you have to do is compare the <code>ActualWidth</code> of the <code>TextBlock</code> to the <code>ActualWidth</code> of its parent.</p>
<p>However, the WPF <code>TextBlock</code> does not have this same quirky behaviour, so a completely different approach is required. The other solutions I have seen involve using the low-level WPF drawing APIs to compute the size of the rendered text, however, there is a simpler solution to this problem &#8230;</p>
<p><strong>UPDATE:</strong> I originally wrote about a method of finding the overall text width from various internal fields within the <code>TextBlock</code> via reflection, as shown below. However, a kind reader of my blog, <strong>Daniel Fihnn</strong>, pointed out that there is a simpler solution that does not require any reflection.</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextBlockInternal.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextBlockInternal-300x128.png" alt="" title="TextBlockInternal" width="300" height="128" class="aligncenter size-medium wp-image-1176" /></a></p>
<p>Daniel pointed out that the width of the TextBlock without the trimming can be obtained using the <code>Measure</code> method. This method is typically used by panels during layout, the <code>Measure</code> method is invoked with the size made available to the element, calling this method causes the element to update its <code>DesiredSize</code> property. Therefore, if you invoke <code>Measure</code> on a <code>TextBlock</code> which has trimming enabled, giving it an infinite available space, its <code>DesiredSize</code> property will report the width that the text would occupy without trimming.</p>
<p>The <code>ComputeAutoTooltip</code> method of the attached behaviour I described in my previous post is updated to have a completely different WPF implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TextBlockUtils
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets the value of the AutoTooltipProperty dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> GetAutoTooltip<span style="color: #008000;">&#40;</span>DependencyObject obj<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>AutoTooltipProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Sets the value of the AutoTooltipProperty dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetAutoTooltip<span style="color: #008000;">&#40;</span>DependencyObject obj, <span style="color: #6666cc; font-weight: bold;">bool</span> value<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    obj<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>AutoTooltipProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Identified the attached AutoTooltip property. When true, this will set the TextBlock TextTrimming</span>
  <span style="color: #008080; font-style: italic;">/// property to WordEllipsis, and display a tooltip with the full text whenever the text is trimmed.</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty AutoTooltipProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AutoTooltip&quot;</span>,
          <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>TextBlockUtils<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span>, OnAutoTooltipPropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnAutoTooltipPropertyChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    TextBlock textBlock <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> TextBlock<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>textBlock <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">TextTrimming</span> <span style="color: #008000;">=</span> TextTrimming<span style="color: #008000;">.</span><span style="color: #0000FF;">WordEllipsis</span><span style="color: #008000;">;</span>
      ComputeAutoTooltip<span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeChanged</span> <span style="color: #008000;">+=</span> TextBlock_SizeChanged<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeChanged</span> <span style="color: #008000;">-=</span> TextBlock_SizeChanged<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> TextBlock_SizeChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, SizeChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    TextBlock textBlock <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBlock<span style="color: #008000;">;</span>
    ComputeAutoTooltip<span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Assigns the ToolTip for the given TextBlock based on whether the text is trimmed</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ComputeAutoTooltip<span style="color: #008000;">&#40;</span>TextBlock textBlock<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
<span style="color: #008080;">#if WPF</span>
    textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Measure</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Size<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Double</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PositiveInfinity</span>, <span style="color: #6666cc; font-weight: bold;">Double</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PositiveInfinity</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var width <span style="color: #008000;">=</span> textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">DesiredSize</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">&lt;</span> width<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
      ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080;">#else</span>
    FrameworkElement parentElement <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetParent</span><span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>parentElement <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">&gt;</span> parentElement<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0600FF; font-weight: bold;">else</span>
      <span style="color: #008000;">&#123;</span>
        ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#endif </span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This code now works in both a WPF and a Silverlight context. Here&#8217;s a screenshot of it working in WPF:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/AutoTooltip.png" alt="" title="AutoTooltip" width="601" height="153" class="aligncenter size-full wp-image-1179" /></p>
<p>You can download the full sourcecode, including the WPF and Silverlight demo: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/AutoTooltipTextBlock2.zip'>AutoTooltipTextBlock.zip</a></p>
<p>Thanks to Daniel Fihnn for a much improved WPF version of this code!</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Does HTML5 mean the end is in sight for Silverlight?</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 21:52:07 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=899</guid>
		<description><![CDATA[This blog post looks at the fallout after last week’s PDC conference where Microsoft were quoted as saying “our strategy on Silverlight has shifted”, and the resulting fallout in the developer community. In this post I will describe why I think Silverlight has a future ahead of it and exactly where that future lies. Bob [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F11%252Fdoes-html5-mean-the-end-is-in-sight-for-silverlight%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Does%20HTML5%20mean%20the%20end%20is%20in%20sight%20for%20Silverlight%3F%20%23%22%20%7D);"></div>
<p><em>This blog post looks at the fallout after last week’s PDC conference where Microsoft were quoted as saying “our strategy on Silverlight has shifted”, and the resulting fallout in the developer community. In this post I will describe why I think Silverlight has a future ahead of it and exactly where that future lies.</em></p>
<h3>Bob Puts His Foot In It!</h3>
<p>Last week’s Microsoft Professional Developer Conference (PDC) has caused quite a stir. It was clear from Steve Ballmer’s keynote speech that HTML5 was taking centre stage, with Silverlight not getting a single mention in his opening speech.</p>
<p>The image below shows a word cloud showing the most frequent words in <a href="http://www.microsoft.com/presspass/exec/steve/2010/10-28PDC10.mspx">Steve Ballmer&#8217;s PDC keynote speech</a>, rendered using <a href="http://www.wordle.net">Wordle</a></p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/wordCloud.png" alt="" title="wordCloud" width="546" height="260" class="aligncenter size-full wp-image-904" /></p>
<p>This is in contrast to previous events such as MIX9 where (just over a year ago) Silverlight 3 took centre stage. This focus on HTML5, which many see as a competing technology to Silverlight was enough to get the community a little worried, however, it was the interview that <a href="http://www.zdnet.com/blog/microsoft/microsoft-our-strategy-with-silverlight-has-shifted/7834">Bob Muglia gave to ZDNet</a> that really caused a storm. </p>
<p>The key quotes are given below:</p>
<p><i>“[...] when it comes to touting Silverlight as Microsoft’s vehicle for delivering a cross-platform runtime, our strategy has shifted.”</i></p>
<p><i>“But HTML is the only true cross platform solution for everything, including (Apple’s) iOS platform”</i></p>
<p>The above statements caused quite a stir, with calls for Bob to clarify his statements which seemed to contradict the recent <a href="http://team.silverlight.net/announcement/the-future-of-silverlight/">“Future of Silverlight”</a> blog post which contrasted the difference between standardisation (HTML5) and innovation (Silverlight) to show a very positive future for the technology. I think this is the first time I had seen such a direct comparison of HTML5 vs. Silverlight from Microsoft’s Silverlight team, and the arguments they made were quite compelling. </p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/silverlightFuture.png" alt="" title="silverlightFuture" width="522" height="188" class="aligncenter size-full wp-image-901" /></p>
<h3>Bob Pulls His Foot Out Again</h3>
<p>As a result of Bob’s interview, which was commented upon by virtually everyone in the Silverlight community (<a href="http://csharperimage.jeremylikness.com/2010/10/so-whats-fuss-about-silverlight.html">Jeremy Likeness</a>, <a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/11/01/silverlight-versus-html5-really.aspx">Mike Taulty</a>, and many many more), Bob quickly <a href="http://team.silverlight.net/announcement/pdc-and-silverlight/">followed up with a blog post</a>  where he stated the following:</p>
<p><i>“I said, ‘Our Silverlight strategy and focus going forward has shifted.’  This isn’t a negative statement, but rather, it’s a comment on how the industry has changed and how we’re adapting our Silverlight strategy to take advantage of that”</i></p>
<p>However, I think that the most important point he made came at the very end of his post:</p>
<p><i>“The purpose of Silverlight has never been to replace HTML, but rather to do the things that HTML (and other technologies) can’t, and to do so in a way that’s easy for developers to use.”</i></p>
<p>I think it is people’s misunderstanding of the differences between Silverlight and HTML that caused most of this uproar in the first place. It is this point which I would like to concentrate on, and hopefully provide a bit more clarity.</p>
<h3>PDC is an Event</h3>
<p>Before I focus on the differences between HTML and Silverlight I think it is worth noting that PDC is an event, and just like any other event it needs to be marketed in order to fuel people’s interest and get them through the door. Microsoft embraces a very wide range of technologies, and if the PDC tried to be a true reflection of this diversity it would be very fragmented and hard to market. It is much better that the event focuses on something new and interesting to provide an underlying theme. This year that theme was HTML5.</p>
<p>Interestingly after MIX9 where Silverlight was the main focus and <a href="http://www.scottlogic.co.uk/blog/colin/2009/03/silverlight-3-is-here-but-is-it-a-flash-killer-yet/">out-of-browser support was announced</a>, there was noticeable unrest amongst the WPF community, they felt short-changed (4 MIX sessions on WPF to Silverlight’s 31) and there was speculation that Silverlight would replace WPF entirely. However, when the knee-jerk reactions were forgotten about and a more sensible analysis of the difference between the two was considered (see <a href="http://10rem.net/blog/2009/12/01/the-future-of-client-app-dev--wpf-and-silverlight-convergence">Pete Brown’s Future of Client App Development blog post</a> for example), it was clear that WPF does have a future. </p>
<h3>The Web Landscape</h3>
<p>Back to HTML and Silverlight, if we look at the current landscape of the internet we can crudely characterise web sites and web based applications based on the degree of interactivity that provide:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/slidingScale.png" alt="" title="slidingScale" width="400" height="163" class="aligncenter size-full wp-image-903" /></p>
<p>In the above diagram we see a sliding scale of interactivity and complexity with a few well known sites positioned along this scale (don’t ask me about what units I am using to measure each site!).</p>
<p>With the above sliding scale we can position the popular internet technologies based on their relative strengths and the types of site they are most suited to:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/slidingScale2.png" alt="" title="slidingScale2" width="400" height="129" class="aligncenter size-full wp-image-902" /></p>
<p>I have not tried to pin each technology to a specific location on the scale because in my opinion the boundaries between each are blurred &#8230; a lot. It is possible to use any of the above technologies to create a web site / web application at either end of the spectrum, however, the further away you move from the ‘comfort zone’ of each technology, the harder the job becomes.</p>
<p>We’ll start at the Flash / Silverlight end of the spectrum. Why not capitalise on the fact that these plugin technologies provide a controlled environment for your code to run in, providing great performance and virtually zero browser compatibility issues?</p>
<p>Whilst this point may not have been addressed in great detail for Silverlight, there are certainly numerous strong arguments presented on the internet which describe quite clearly why full-page Flash sites will never fully replace HTML (For a thorough analysis take a look at Emil Stenström’s blog post on <a href="http://friendlybit.com/js/flash-only-vs-ajax-sites/">Flash vs. Ajax sites</a>). The main reasons cited are:</p>
<ul>
<li>Splash screens – Flash / Silverlight sites need to load the entire ‘executable’ before they can render. Contrast this with how quickly the pages from Wikipedia appear.</li>
<li>Maintenance – Quick fixes are often required for websites, with HTML / JavaScript this can often be left to the support team. For Silverlight / Flash this requires a full rebuild of the application.</li>
</ul>
<p>(There are a number of other points, including search engine indexing, etc &#8230;)</p>
<p>There is not a strong case for replacing simpler web sites with Silverlight. I think my fellow WPF Disciple <a href="http://justinangel.net/">Justin Angel’s blog</a>, which is powered entirely with Silverlight is unfortunately a good illustration of why Silverlight should not be used to replace HTML for the presentation of largely static content. Sitting here on a train using the pitiful WiFi connection I can see Justin’s stylish loading ‘splash screen’, however, after I minute of waiting I grow tired of staring at his beard and sunglasses and little else! </p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/loading.png" alt="" title="loading" width="536" height="316" class="aligncenter size-full wp-image-900" /></p>
<p>On the flip-side, when HTML and JavaScript technologies are used to create complex applications, development becomes costly and complex. Cross-browser problems, lack of support for business concepts such as controls and validation, performance issues all start to have a significant impact on the development. To combat this more and more sophisticated solutions are being applied, from the numerous libraries that abstract the differences in browser DOM APIs (jQuery etc&#8230;) to <a href="http://en.wikipedia.org/wiki/Google_Web_Toolkit">Google Web Toolkit</a>, which generates JavaScript code from Java (which provides strong typing and better structuring).</p>
<p>Interestingly in the book <a href="http://oreilly.com/catalog/0636920001416/">Google Closure the Definitive Guide</a>, the preface describes how Google Closure (a JavaScript compiler and mechanism for checking types) was developed to combat the problems the Google Mail development team were experiencing in developing a complex JavaScript application composed of tens of thousands of lines of JavaScript. The take-home message of this is that for complex applications you need more than the JavaScript language alone provides.</p>
<h3>Bob’s Comments in Perspective</h3>
<p>In the previous section I described how the web landscape can be illustrated as a sliding scale of complexity, with HTML and Silverlight at opposite ends of the spectrum and HTML5 sitting somewhere in-between. With this in mind, Silverlight developers should not fear that HTML5 will replace Silverlight. Whilst it is true that there is some common ground between the two, i.e. a certain interactivity / web-site complexity where the two technologies are equally suited, Silverlight users should feel comfortable that for the more complex problems, Silverlight has a clear advantage.</p>
<p>To repeat Bob’s closing statement of his follow-up blog post:</p>
<p><i>“The purpose of Silverlight has never been to replace HTML, but rather to do the things that HTML (and other technologies) can’t, and to do so in a way that’s easy for developers to use.”</i></p>
<p>I think it is the failure of the developer community to realise that there is little direct competition between HTML5 and Silverlight and failure of Microsoft’s marketing to highlight this.</p>
<h3>The Web Increases its Reach.</h3>
<p>However, there are some important points to note in Bob’s comments and his reference to iOS (i.e. iPhone, iPad etc&#8230;). Whilst I do not think HTML5 is a significant threat to Silverlight for web based application development, the introduction of a more diverse range of mobile devices potentially increase the market for HTML.</p>
<p>Most people who use an Android phone or an iPad do not use them as a replacement for their laptop or desktop PC. They use them on the train, in the coffee shop and at home on the sofa as a way to stay online and connected for more of their waking hours. HTML5 is a suitable technology for delivering a interactivity to a diverse range of devices and it looks like the overall market size is increasing.</p>
<p>However, if Microsoft can produce Silverlight plugins for these devices, on the Android phone for example, there is no reason why Silverlight cannot be part of this growing market, delivering more than just interactivity to the mobile platform.</p>
<p>Unfortunately, more recent news, like today’s announcement by the Bing Maps team that seems to indicates they will be <a href="http://www.bing.com/community/site_blogs/b/maps/archive/2010/11/02/changes-to-bird-s-eye-and-3d-maps.aspx">dropping their Silverlight based 3D libraries in favour of HTML5</a> is going to fuel the fire which Bob and Steve Ballmer are currently trying to dowse.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>White Paper: Silverlight, WPF and Windows Phone 7 cross platform development</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 09:10:25 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=829</guid>
		<description><![CDATA[Last week I gave a presentation at a joint Scott Logic / Microsoft event about how WPF and Silverlight are unifying the development platform for desktop, web and mobile. To accompany the talk I wrote a white paper which delves into this subject in a little more detail. You can download the whitepaper here: WPF [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F10%252Fwhite-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22White%20Paper%3A%20Silverlight%2C%20WPF%20and%20Windows%20Phone%207%20cross%20platform%20development%20%23%22%20%7D);"></div>
<p>Last week <a href="http://www.scottlogic.co.uk/2010/10/unifying-development-across-the-desktop-web-and-mobile-scott-logic-points-the-way/">I gave a presentation at a joint Scott Logic / Microsoft event</a> about how WPF and Silverlight are unifying the development platform for desktop, web and mobile. To accompany the talk I wrote a white paper which delves into this subject in a little more detail.</p>
<p>You can download the whitepaper here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/10/WPF-and-Silverlight-Unification.pdf'>WPF and Silverlight Cross Platform Development White Paper</a> (PDF, 1.8 MBytes)</p>
<p>Or view it online in the PDF viewer below: </p>
<p><iframe src="http://docs.google.com/viewer?url=http%3A%2F%2Fwww.scottlogic.co.uk%2Fblog%2Fcolin%2Fwp-content%2Fuploads%2F2010%2F10%2FWPF-and-Silverlight-Unification.pdf&#038;embedded=true" width="600" height="780" style="border: none;"></iframe></p>
<p>I also created a cross platform application that has a 75% shared codebase between the WPF (desktop), Silverlight (web) and Windows Phone 7 (mobile) versions. I plan to share more of this at a later date.</p>
<p>Any thoughts, ideas or comments, please share them below.</p>
<p>Regards,<br />
Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Templates, or Why I love WPF (and Silverlight Too!)</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/09/templates-or-why-i-love-wpf-and-silverlight-too/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/09/templates-or-why-i-love-wpf-and-silverlight-too/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 12:22:33 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=780</guid>
		<description><![CDATA[This post compares the implementation of a simple ListBox layout with Windows Forms and Windows Presentation Foundation. The use of Templates within WPF are a clear winner over the WinForms &#8216;owner draw&#8217; route. Application user interfaces are becoming much more graphical, with users expecting a more engaging and ‘lively’ experience. However, rich graphical UIs can [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F09%252Ftemplates-or-why-i-love-wpf-and-silverlight-too%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Templates%2C%20or%20Why%20I%20love%20WPF%20%28and%20Silverlight%20Too%21%29%20%23%22%20%7D);"></div>
<p><em>This post compares the implementation of a simple ListBox layout with Windows Forms and Windows Presentation Foundation. The use of Templates within WPF are a clear winner over the WinForms &#8216;owner draw&#8217; route.</em></p>
<p>Application user interfaces are becoming much more graphical, with users expecting a more engaging and ‘lively’ experience. However, rich graphical UIs can do much more for the user than just provide an experience that looks and feels good. Graphics can be used to more effectively convey the options available to the user. For example, which of these line weight selectors would assist best in helping a a user determine a suitable selection &#8230;</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/lineWeight.png" alt="" title="lineWeight" width="600" height="189" class="aligncenter size-full wp-image-781" /></p>
<p>I think most users would prefer the second option where they can visualise a range of line weights and actually see how they look (after all, how many users know what a &#8216;pt&#8217; actually is?) before making a selection. However, for a Windows Forms developer, the first option, a simple text box with the label &#8216;pt&#8217; next to it, is trivial to implement, whereas the second is not.</p>
<p>Applications such as Word 2007 present as many of the user options as possible in a visual style, for example, the font selector in previous versions of Word simply presented a list of font names, whereas in Word 2007 each font is rendered as it would appear to the user. Furthermore, the font selector combo-box is grouped to include recently used fonts:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/fontSelector.png" alt="" title="fontSelector" width="501" height="294" class="aligncenter size-full wp-image-783" /></p>
<p>Unfortunately for the developer, creating visual UIs can be quite challenging. </p>
<p>Windows Forms has been the technology of choice for the creation of business desktop applications for a number of years. The developer is presented with a large number of standard controls with which they can build their application, however these controls lack the flexibility required to create a more visual interface. For example, combo-boxes and list-boxes are designed to render text content, not mixed font text with icons, or lines of varying weight as seen in our above examples. To create a control that renders non-standard content, the only route available to the developer is &#8216;owner draw&#8217; where the developer handles events in order to render all or part of the control. This is a time consuming and highly iterative process, with the developer having to compile and run their code repeatedly to see the results of their graphics code.</p>
<p>One of the key differences between Windows Forms and WPF is the way in which the visual representation of controls and their content is encoded. WPF has the concept of templates, an XML (or more accurately XAML) representation of how a control is rendered to the screen. This is what allows WPF developers / designers to create <a href="http://www.codeproject.com/KB/WPF/glassbuttons.aspx">funky looking round buttons</a> and other impressive UI fluff! However, probably of more interest to developers of business applications are data templates, which detail how data is represented on the screen. </p>
<p>Controls such as list-boxes and combo-boxes are used to represent, and provide selection from, a list of data objects. With the WPF framework, the way in which this data is represented is specified by a DataTemplate. We&#8217;ll take a look at how this differs from the WinForms owner-draw route by rendering the same content with each technology.</p>
<p>There are quite a few articles available on the internet which describe how to create an owner draw listbox. I took the sourcecode from an <a href="http://www.codeproject.com/KB/combobox/exListBox.aspx">article on codeproject by GiedriusBan</a>, his article describes how to owner-draw a listbox to add some more complex content into each item:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/winformsownerdraw.png" alt="" title="winformsownerdraw" width="471" height="298" class="aligncenter size-full wp-image-790" /></p>
<p>Because the listbox is being used to render more than just text, the only viable solution is to &#8216;owner draw&#8217; each listbox item, this is done by handling an event provided by the listbox and the following GDI graphics code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> drawItem<span style="color: #008000;">&#40;</span>DrawItemEventArgs e, Padding margin, 
                        Font titleFont, Font detailsFont, StringFormat aligment, 
                        Size imageSize<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> isHighlighted <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">&amp;</span> DrawItemState<span style="color: #008000;">.</span><span style="color: #0000FF;">Selected</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> DrawItemState<span style="color: #008000;">.</span><span style="color: #0000FF;">Selected</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// if selected, mark the background differently</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FillRectangle</span><span style="color: #008000;">&#40;</span>isHighlighted <span style="color: #008000;">?</span> Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">DarkBlue</span> <span style="color: #008000;">:</span> Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
    <span style="color: #008080; font-style: italic;">// draw some item separator</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawLine</span><span style="color: #008000;">&#40;</span>Pens<span style="color: #008000;">.</span><span style="color: #0000FF;">DarkGray</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>,
                               e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// draw item image</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawImage</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ItemImage</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span>,
                                  e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span>, imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// draw a border around the image</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawRectangle</span><span style="color: #008000;">&#40;</span>Pens<span style="color: #008000;">.</span><span style="color: #0000FF;">DarkGray</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span>,
                                       imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// calculate bounds for title text drawing</span>
    Rectangle titleBounds <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Horizontal</span> <span style="color: #008000;">+</span> imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>,
                  e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span>,
                  e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span> <span style="color: #008000;">-</span> imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Horizontal</span>,
                  <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>titleFont<span style="color: #008000;">.</span><span style="color: #0000FF;">GetHeight</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// calculate bounds for details text drawing</span>
    Rectangle detailBounds <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Horizontal</span> <span style="color: #008000;">+</span> imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>,
                    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>titleFont<span style="color: #008000;">.</span><span style="color: #0000FF;">GetHeight</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Vertical</span> <span style="color: #008000;">+</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span>,
                    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span> <span style="color: #008000;">-</span> imageSize<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Horizontal</span>,
                    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Bounds</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Bottom</span> <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>titleFont<span style="color: #008000;">.</span><span style="color: #0000FF;">GetHeight</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Vertical</span> <span style="color: #008000;">-</span> margin<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// draw the text within the bounds</span>
    var brush <span style="color: #008000;">=</span> isHighlighted <span style="color: #008000;">?</span> Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span> <span style="color: #008000;">:</span> Brushes<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">;</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span>, titleFont, brush, titleBounds, aligment<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">Graphics</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DrawString</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Details</span>, detailsFont, brush, detailBounds, aligment<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>            
&nbsp;
    <span style="color: #008080; font-style: italic;">// put some focus rectangle</span>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">DrawFocusRectangle</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This is a lot of &#8216;heavy&#8217; and hard to follow graphics code to achieve such a simple listbox item layout!</p>
<p>With WPF the listbox uses a template to render the data within the list. Here is the same list rendered with a WPF application:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/wpftemplates.png" alt="" title="wpftemplates" width="508" height="286" class="aligncenter size-full wp-image-791" /></p>
<p>And here is the XAML code to create this layout:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">ItemContainerStyle</span>=<span style="color: #ff0000;">&quot;{StaticResource StretchedContainerStyle}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;Auto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;Auto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span>  <span style="color: #000066;">Grid.RowSpan</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;LightGray&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,0,5,0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding Image}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Name}&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">FontWeight</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,0,0,5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Details}&quot;</span></span>
<span style="color: #009900;">                                <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Line</span> <span style="color: #000066;">X1</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Y1</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">X2</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Y2</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Stretch</span>=<span style="color: #ff0000;">&quot;Uniform&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;DarkGray&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Comparing the two approaches; not only is there less code in the WPF version, it is also much clearer and easy to follow. However, the advantages of the WPF approach do not end there &#8230;</p>
<p>WPF uses a <a href="http://en.wikipedia.org/wiki/Retained_mode">retained graphics mode</a>, which means the various elements in the above XAML code are assembled into a visual tree that is retained in memory, rather than just being painted to some bitmap. This means that the developer does not have to worry about invalidation, and does not need to employ techniques to avoid flicker when re-drawing. </p>
<p>The original author of the WinForms code above subclassed ListBox and created a concrete control for rendering this <em>specific </em>data with this <em>specific </em>layout. With WPF there is no subclassing, and no need to create a specific ListBoxItem for each row to provide the data for the control, instead the list binds directly to the business objects, with the DataTemplate doing all the work.</p>
<p>Finally, you can supply design time data to your WPF / Silverlight applications, allowing you to get immediate feedback when designing your templates, removing the time consuming build / execution cycle required with WinForms. The screenshot below shows how the listbox looks in visual studio:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/designTimeData.png" alt="" title="designTimeData" width="568" height="529" class="aligncenter size-full wp-image-793" /></p>
<p>And here is the design time data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PersonCollection</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;clr-namespace:TemplatedListBox&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Person</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;John, the Tester&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Details</span>=<span style="color: #ff0000;">&quot;First details text is used to check it out&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Image</span>=<span style="color: #ff0000;">&quot;image1.jpg&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Person</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;John, the Tester&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Details</span>=<span style="color: #ff0000;">&quot;First details text is used to check it out&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Image</span>=<span style="color: #ff0000;">&quot;image2.jpg&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PersonCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In conclusion, templates are a very powerful features of the WPF (and Silverlight) framework that provide far more that just a mechanism for creating round buttons! If we look at the font selector used in Word 2007, I can imagine creating this UI with WPF in a matter of hours, whereas with WinForms it would take days. That is why I love WPF!</p>
<p>You can download the WPF and WinForms source for this article: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/09/WPFLove.zip'>WPFLove.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/09/templates-or-why-i-love-wpf-and-silverlight-too/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Silverlight MultiBinding updated, adding support for ElementName and TwoWay binding</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 20:11:39 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[multibinding]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[virtual branch]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=755</guid>
		<description><![CDATA[This blog post describes an update to the Silverlight 4 MultiBinding technique I blogged about a couple of months ago to add support for ElementName binding and TwoWay binding. A few months ago I posted an update to my MultiBinding solution for Silverlight 4. This technique allows you to perform the same kind of multibindings [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F08%252Fsilverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Silverlight%20MultiBinding%20updated%2C%20adding%20support%20for%20ElementName%20and%20TwoWay%20binding%20%23%22%20%7D);"></div>
<p><em>This blog post describes an update to the Silverlight 4 MultiBinding technique I blogged about a couple of months ago to add support for ElementName binding and TwoWay binding.</em></p>
<p>A few months ago I posted an update to my MultiBinding solution for Silverlight 4. This technique allows you to perform the same kind of multibindings which are possible in WPF, where a property value is bound to multiple sources via a special value converter that implements the IMultiValueConverter interface, which describes how these values are combined. This update proved popular once again, and I received a few requests to add support for ElementName and TwoWay binding. I like challenge! This blog post describes how these two features were implemented, but if you just want to grab the code, you will find the link at the end of this article.</p>
<p><strong>A brief recap</strong></p>
<p>Before I go into the implementation details for the new features I will provide a brief recap of how my multibinding solution works. The XAML for creating a multibinding looks like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;White&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000000; font-weight: bold;">&lt;local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBinding</span> <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;Text&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">Converter</span>=<span style="color: #ff0000;">&quot;{StaticResource TitleConverter}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Surname&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Forename&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBinding</span> <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;ToolTipService.ToolTip&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">Converter</span>=<span style="color: #ff0000;">&quot;{StaticResource TitleConverter}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Surname&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Forename&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here the Text property and attached ToolTip property of the TextBlock are bound to both the Surname and Forename property of the business object which is set as the DataContext of our view.</p>
<p>The value converter is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TitleConverter <span style="color: #008000;">:</span> IMultiValueConverter
<span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> values, Type targetType,
    <span style="color: #6666cc; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> forename <span style="color: #008000;">=</span> values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> surname <span style="color: #008000;">=</span> values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}, {1}&quot;</span>, surname, forename<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In the example below you can see that as you change the forename or surname, the title text and tooltip are updated by the multibinding.</p>
<div> <object width="200" height="150" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/08/SLMultiBindingOne.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>So how does this work?</p>
<p>When a multibinding is created and added to an element via the BindingUtil.MultiBindings attached property, the MultiBinding instance is added to a virtual-branch, this is a branch of the visual tree that obtains the DataContext of the element to which it is bound, but is not added to the visual tree itself. The MultiBinding then creates a BindingSlave instance for each of the given Bindings. These ‘slave’ elements inherit the MultiBinding DataContext and are used to evaluate each of the individual Bindings. The MultiBinding instance aggregate the results of each binding with the IMultiValueConverter used to compute the ConvertedValue property which is bound to the target property on the element to which this MultiBinding as attached.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/06/virtualbranch.png" alt="" title="virtualbranch" width="604" height="394" class="aligncenter size-full wp-image-355" /></p>
<p><strong>ElementName binding</strong></p>
<p>So why does the above solution not work if one of the multibindings uses ElementName to locate the source rather than using the inherited DataContext? The problem here is that the MultiBinding and its BindingSlave instances are located in a virtual branch and are therefore not in the same <a href="http://msdn.microsoft.com/en-us/library/cc189026%28VS.95%29.aspx">namescope</a> as the target element. As a result, they cannot perform look up of named elements.</p>
<p>In order to solve this problem I created a BindingSlave subclass specifically for ElementName binding. This slave locates the source element named via the ElementName property and sets it as the Source of the binding it uses to compute its Value property.</p>
<p>The only minor complication I encountered is that the MultiBinding might be constructed before the element referenced via ElementName. For this reason the binding slave must handle LayoutUpdated events to ensure that the named element is located if it is constructed after the multibinding.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ElementNameBindingSlave <span style="color: #008000;">:</span> BindingSlave
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> FrameworkElement _multiBindingTarget<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// The source element named in the ElementName binding</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> FrameworkElement _elementNameSource<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> Binding _binding<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ElementNameBindingSlave<span style="color: #008000;">&#40;</span>FrameworkElement target, Binding binding<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _multiBindingTarget <span style="color: #008000;">=</span> target<span style="color: #008000;">;</span>
    _binding <span style="color: #008000;">=</span> binding<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// try to locate the named element</span>
    ResolveElementNameBinding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _multiBindingTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">LayoutUpdated</span> <span style="color: #008000;">+=</span> MultiBindingTarget_LayoutUpdated<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Try to locate the named element. If the element can be located, create the required</span>
  <span style="color: #008080; font-style: italic;">/// binding.</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ResolveElementNameBinding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _elementNameSource <span style="color: #008000;">=</span> _multiBindingTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">FindName</span><span style="color: #008000;">&#40;</span>_binding<span style="color: #008000;">.</span><span style="color: #0000FF;">ElementName</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_elementNameSource <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      SetBinding<span style="color: #008000;">&#40;</span>BindingSlave<span style="color: #008000;">.</span><span style="color: #0000FF;">ValueProperty</span>, <span style="color: #008000;">new</span> Binding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        Source <span style="color: #008000;">=</span> _elementNameSource,
        Path <span style="color: #008000;">=</span> _binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span>,
        Converter <span style="color: #008000;">=</span> _binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Converter</span>,
        ConverterParameter <span style="color: #008000;">=</span> _binding<span style="color: #008000;">.</span><span style="color: #0000FF;">ConverterParameter</span>
      <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> MultiBindingTarget_LayoutUpdated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// try to locate the named element </span>
    ResolveElementNameBinding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The example below demonstrates element name binding by binding the Value of two Slider elements to a TextBlock which displays the sum of the two values.</p>
<div> <object width="200" height="150" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/08/SLMultiBindingTwo.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBinding</span> <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;Text&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">Converter</span>=<span style="color: #ff0000;">&quot;{StaticResource SliderValueConverter}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">ElementName</span>=<span style="color: #ff0000;">&quot;sliderOne&quot;</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Value&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">ElementName</span>=<span style="color: #ff0000;">&quot;sliderTwo&quot;</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Value&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Slider</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;sliderOne&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Slider</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;sliderTwo&quot;</span>  <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Here is the value converter that is used to compute the sum:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> SliderValueConverter <span style="color: #008000;">:</span> IMultiValueConverter
<span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> values, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>values<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">||</span>
        values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span>
        values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You could of course use element name binding to combine the surname / forename in the first example in this blog post, but in this context it is more likely that you will want to back your UI with a business object. It is also possible to mix element name and regular (i.e. Source=DataContext) binding.</p>
<p>I find multibindings which use ElementName references most useful when creating complex UI layouts that do not lend themselves to being implemented using Panels. For a good example see my article on <a href="http://www.codeproject.com/KB/WPF/WpfWinFormsBulletGraphs.aspx">the implementation of a BulletGraph control </a>which makes extensive use of this technique in order that the layout of the control is performed entirely within XAML.</p>
<p><strong>TwoWay Binding</strong></p>
<p>A two-way multibinding must be able to handle updates to the combined value and split it up into its constituent parts in order to update the multiple source bindings. In the first example where a forename and surname property are combined into a “surname, forename” string it is possible to convert back the other way, but in the example above where the values of two Sliders are combined a reverse conversion is not possible.</p>
<p>Modifying this multibinding solution to permit two way binding was relatively straightforward. A property changed event handler was added to the MultiBindig.ConverterValue (which is bound to the multi binding target) so that we can determine when changes have been made. This handler then uses the IMultiValueConverter to convert the value into the multiple source properties and updates the binding slaves accordingly:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Handles property changes for the ConvertedValue property</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnConvertedValuePropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ConvertedValue&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// if the value is being updated, but not due to one of the multibindings</span>
    <span style="color: #008080; font-style: italic;">// then the target property has changed.</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>_updatingConvertedValue<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// convert back</span>
      <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> convertedValues <span style="color: #008000;">=</span> Converter<span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertBack</span><span style="color: #008000;">&#40;</span>ConvertedValue, <span style="color: #0600FF; font-weight: bold;">null</span>,
          ConverterParameter, CultureInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">InvariantCulture</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// update all the binding slaves</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Children<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> convertedValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> index <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> index <span style="color: #008000;">&lt;</span> convertedValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> index<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
          <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>BindingSlave<span style="color: #008000;">&#41;</span>Children<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> convertedValues<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In order to use two way multibinding both the multibinding mode <em>and</em> the bindings within the BindingCollection must be set to TwoWay. See the example given below:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Surname:&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span>  <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Surname, Mode=TwoWay}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Forename:&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span>  <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Forename, Mode=TwoWay}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Combined:&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span>  <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MultiBinding</span> <span style="color: #000066;">TargetProperty</span>=<span style="color: #ff0000;">&quot;Text&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">Converter</span>=<span style="color: #ff0000;">&quot;{StaticResource TitleConverter}&quot;</span></span>
<span style="color: #009900;">                        <span style="color: #000066;">Mode</span>=<span style="color: #ff0000;">&quot;TwoWay&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Surname&quot;</span> <span style="color: #000066;">Mode</span>=<span style="color: #ff0000;">&quot;TwoWay&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binding</span> <span style="color: #000066;">Path</span>=<span style="color: #ff0000;">&quot;Forename&quot;</span> <span style="color: #000066;">Mode</span>=<span style="color: #ff0000;">&quot;TwoWay&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:BindingUtil.MultiBindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can have  try of two way multibinding below where the values of the forename and surname text boxes are combined in the box below. However, you ca also make updates to the combined result which will then be converted back into its constituent parts:</p>
<div> <object width="300" height="150" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/08/SLMultiBindingThree.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>And here is the value converter:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TitleConverter <span style="color: #008000;">:</span> IMultiValueConverter
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080;">#region IMultiValueConverter Members</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> values, Type targetType,
    <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> forename <span style="color: #008000;">=</span> values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> surname <span style="color: #008000;">=</span> values<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}, {1}&quot;</span>, surname, forename<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> targetTypes,
    <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> source <span style="color: #008000;">=</span> value <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
    var pos <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;, &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> forename <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>pos <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> surname <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, pos<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> forename, surname <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So, there you have it, two-way and element name multibinding <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>You can download the full source, including a WPF build (thanks to <a href="http://www.olsonsoft.com/blogs/stefanolson/">Stefan Olson</a>) here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/08/SLMultiBinding.zip'>SLMultiBinding.zip</a> </p>
<p>Now &#8230; go forth and multibind.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/08/silverlight-multibinding-updated-adding-support-for-elementname-and-twoway-binding/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
		<item>
		<title>A Universal Value Converter for WPF</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/07/a-universal-value-converter-for-wpf/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/07/a-universal-value-converter-for-wpf/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 11:33:18 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[valueconverter]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=726</guid>
		<description><![CDATA[This post provides a simple IValueConverter implementation that makes use of the framework type converters in order to convert between a large range of source / target types. This converter can be used both within bindings and in code-behind to give more concise property setters. Introduction One of the great features of the XAML language [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2010%252F07%252Fa-universal-value-converter-for-wpf%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22A%20Universal%20Value%20Converter%20for%20WPF%20%23%22%20%7D);"></div>
<p><em>This post provides a simple IValueConverter implementation that makes use of the framework type converters in order to convert between a large range of source / target types. This converter can be used both within bindings and in code-behind to give more concise property setters.</em></p>
<h2>Introduction</h2>
<p>One of the great features of the XAML language is that it is flexible, concise and expressive (yes, I know that XML can be a little verbose, but if you try to create a complex UI purely in code-behind I think you will agree with my observations!). For example, you can set the fill of a rectangle by simply specifying the named color:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;Green&quot;</span>/<span style="color: #66cc66;">&#41;</span></span></pre></div></div>

<p>or &#8230; you can specify the RGB values directly:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;#00FF00&quot;</span>/<span style="color: #66cc66;">&#41;</span></span></pre></div></div>

<p>Looking at the above examples, you might be fooled into thinking that the Fill property is of type Color. People who are new to WPF often find that this is not the case the first time they try to bind a property of type Color to the Fill property (they would never set the Fill property directly in code behind, because that would be a cardinal sin!). The Fill property is actually of type Brush, and the XAML parser is performing some cunning type conversions in order to make the above markup work.</p>
<p>The solution to this problem of binding a Color to the Fill property is to create a value converter:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ColorToBrushConverter <span style="color: #008000;">:</span> IValueConverter
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>value <span style="color: #008000;">is</span> Color<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Color<span style="color: #008000;">&#41;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The method or operation is not implemented.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Which can be used as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;{Binding Path=MyColorProperty,</span>
<span style="color: #009900;">                                  Converter={StaticResource ColorToBrushConverter}} &quot;</span>/<span style="color: #66cc66;">&#41;</span></span></pre></div></div>

<p>If you search google for <a href="http://www.google.co.uk/search?q=colortobrushconverter">ColorToBrushConverter</a>, you can see that there are a great many people who have implemented this simple little converter. But what happens if you want to bind to a string representation of color? or you want to bind to a stroke dash property or path geometry? Whilst value converters are simple to implement, it is a shame that you have to create so many of them!</p>
<h2>A Universal Value Converter</h2>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/07/816000_pocket_knife.jpg" alt="" title="816000_pocket_knife" width="300" height="199" class="aligncenter size-full wp-image-728" /></p>
<p>Wouldn&#8217;t it be great to have a single value converter that has the same flexibility as the XAML parser? It is actually very simple to create such as converter (and after creating probably my 5th ColorToBrushConverter I have no idea why it took so long before I realised this!). The .NET framework has had an API for conversion between different types via <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx">TypeConverters</a> for a long time. They used extensively in .NET technologies for databinding and designer support, and much more.</p>
<p>A value converter can obtain a suitable TypeConverter for the target property then perform the required conversion:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> UniversalValueConverter <span style="color: #008000;">:</span> IValueConverter
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// obtain the conveter for the target type</span>
        TypeConverter converter <span style="color: #008000;">=</span> TypeDescriptor<span style="color: #008000;">.</span><span style="color: #0000FF;">GetConverter</span><span style="color: #008000;">&#40;</span>targetType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">try</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// determine if the supplied value is of a suitable type</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>converter<span style="color: #008000;">.</span><span style="color: #0000FF;">CanConvertFrom</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// return the converted value</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> converter<span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertFrom</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// try to convert from the string representation</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> converter<span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertFrom</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> value<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, CultureInfo culture<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note, that this value converter first tries to convert directly from the source to the target type, if this is not possible it then tried to convert via a string representation (I am not sure whether it is more correct to obtain a TypeConverter for the String type, then use this rather that invoking ToString on the value being converted, but the above works for me <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ).</p>
<p>You can see this converter in action below where a range of type conversions are demonstrated:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Converting String to Brush ....&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=colorTextBox, Path=Text, Converter={StaticResource UniversalValueConverter}}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;colorTextBox&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Red&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Converting String to Geometry ....&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Path</span> <span style="color: #000066;">Data</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=geometryText, Path=Text, Converter={StaticResource UniversalValueConverter}}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;geometryText&quot;</span>                  </span>
<span style="color: #009900;">             <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;M 10,20 C 10,2.5 40,35 40,17 H 28&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Converting String to DoubleCollection (stroke dash) ....&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Line</span> <span style="color: #000066;">StrokeDashArray</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=dashText, Path=Text, Converter={StaticResource UniversalValueConverter}}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;dashText&quot;</span>                  </span>
<span style="color: #009900;">            <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;2 2 4 5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/07/universal.png" alt="" title="universal" width="395" height="345" class="aligncenter size-full wp-image-735" /></p>
<p>For the first conversion, string to brush, you can use named colors, and the hex notation in its range of abbreviated forms (#AF7, #AAFF77, #FFAAFF77 &#8230;). You can also use this converter to convert from string to their corresponding enum values, for example binding the string &#8220;Collapsed&#8221; to the Visbility property.</p>
<h2>Value conversion in code behind</h2>
<p>The above converter really is swiss army knife for bindings, but what about code-behind? You are still constrained by the type requirements of the property being set:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">rect1<span style="color: #008000;">.</span><span style="color: #0000FF;">Fill</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Color <span style="color: #008000;">=</span> Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Value converters, whilst typically used in binding, can also be used directly in code-behind. The following extension method extends <code>SetValue</code> method for setting dependency properties to make use of the above value converter:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Sets the given dependency property, applying type conversion where required</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetValueEx<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> DependencyObject element, DependencyProperty property, <span style="color: #6666cc; font-weight: bold;">object</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var conv <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UniversalValueConverter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var convertedValue <span style="color: #008000;">=</span> conv<span style="color: #008000;">.</span><span style="color: #0000FF;">Convert</span><span style="color: #008000;">&#40;</span>value, property<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, CultureInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">InvariantCulture</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>property, convertedValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Which provides a more flexible mechanism for setting property values:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">rect1<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValueEx</span><span style="color: #008000;">&#40;</span>Rectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">FillProperty</span>, Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
rect2<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValueEx</span><span style="color: #008000;">&#40;</span>Rectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">FillProperty</span>, <span style="color: #666666;">&quot;Blue&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
rect3<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValueEx</span><span style="color: #008000;">&#40;</span>Rectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">FillProperty</span>, <span style="color: #666666;">&quot;#FFEE55&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
rect4<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValueEx</span><span style="color: #008000;">&#40;</span>Rectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">FillProperty</span>, <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Orange</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h2>&#8230; and Silverlight?</h2>
<p>Unfortunately Silverlight lacks the TypeDescriptor class which is used to obtain TypeConveters. I am guessing that type conversion within Silverlight is &#8216;baked-in&#8217; to the XAML parser, which means that it is not possible to re-use this logic <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>You can download the full source for this blog post: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/07/UniversalValueConverter.zip'>UniversalValueConverter.zip</a></p>
<p>Regards,<br />
Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/07/a-universal-value-converter-for-wpf/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

