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

<channel>
	<title>Avni&#039;s</title>
	<atom:link href="http://amiravni.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://amiravni.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Wed, 03 Mar 2010 18:30:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='amiravni.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Avni&#039;s</title>
		<link>http://amiravni.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://amiravni.wordpress.com/osd.xml" title="Avni&#039;s" />
	<atom:link rel='hub' href='http://amiravni.wordpress.com/?pushpress=hub'/>
		<item>
		<title>DataTemplateSelector &#8230; What the hell ?</title>
		<link>http://amiravni.wordpress.com/2010/03/02/datatemplateselector-what-the-hell/</link>
		<comments>http://amiravni.wordpress.com/2010/03/02/datatemplateselector-what-the-hell/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 17:49:52 +0000</pubDate>
		<dc:creator>amiravni</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[DataTemplate]]></category>
		<category><![CDATA[DataTemplateSelector]]></category>

		<guid isPermaLink="false">http://amiravni.wordpress.com/?p=3</guid>
		<description><![CDATA[Often just the length of the word DataTemplateSelector makes us not read it all the way through, not to mention actually use it&#8230; However, this is actually a very simple and helpful tool. Sometimes we need to display different controls based on a user selection, or display different objects in the same container. Each display [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=amiravni.wordpress.com&amp;blog=12336291&amp;post=3&amp;subd=amiravni&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Often just the length of the word DataTemplateSelector makes us not read it all the way through, not to mention actually use it&#8230;<br />
However, this is actually a very simple and helpful tool.<br />
Sometimes we need to display different controls based on a user selection, or display different objects in the same container. Each display will have its own DataTemplate<br />
however we need a way to be able to determine which DataTemplate to use according to some kind of parameter set and this is exactly where our Selector comes in to play&#8230; I would love to tell you that this eliminates the need for any code behind whatsoever but that is not the case, the implementation of the DataTemplateSelector will require some code however usually this consists of very simple and basic logic (which also makes it very easy to UnitTest).</p>
<p>We will be using the DataTemplateSelector class which exists in the System.Windows.Controls namespace of the PresentationFramework assembly. This class contains just one method:</p>
<p><code><span style="color:#0000FF;">public</span>&nbsp;<span style="color:#0000FF;">virtual</span>&nbsp;<span style="color:#2B91AF;">DataTemplate</span>&nbsp;SelectTemplate(<span style="color:#0000FF;">object</span>&nbsp;item,&nbsp;<span style="color:#2B91AF;">DependencyObject</span>&nbsp;container)<span><br />
</code></p>
<p>You will notice that the method is a virtual one enabling deriving classes to override it and provide their own implementation. For some reason the standard DataTemplateSelector class can be instantiated and the implementation of the SelectTemplate method simply returns null. If anyone knows why this is I would love to hear.</p>
<p>So basicly we have one method which accepts two parameters. An item which is the object according to which we plan to make our DataTemplate selection and a container which is the DependencyObject which will contain the DataTemplate. Lets take the following SoccerPlayer abstract base class containing these two properties:</p>
<p><code><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> Number { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Name { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
</code></p>
<p>And two concrete classes deriving from it, a Midfielder and a Defender:</p>
<p><code><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Defender</span> : <span style="color:#2b91af;">SoccerPlayer<br />
{<br />
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> TacklingRating { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> Strength { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
}<br />
</span></code></p>
<p><code><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Midfielder</span> : <span style="color:#2b91af;">SoccerPlayer<br />
{<br />
<span style="color:#0000ff;"> public</span> <span style="color:#0000ff;">int</span> PassingAccuracy { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
}<br />
</span></code></p>
<p><code><span style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;">Each adding its own special properties. Now we have a situation were we display a list of SoccerPlayer which will show the common properties (Number, Name) and whenever a selection is made in the list we want to display the extended details in a different section of the view (master-detail). We want the detailed display to change according to the type of the actual SoccerPlayer selected, after all, each type will require different labels and bindings in order to properly display all the information.</span></code></p>
<p><code> </code></p>
<p>We will create unique DataTemplates for each of the player types:</p>
<p><code><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">DataTemplate</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">="MidfielderTemplate"&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Foreground</span><span style="color:#0000ff;">="Red"&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="Passing Accuracy:" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=PassingAccuracy}"<br />
<span style="color:#ff0000;"> Margin</span><span style="color:#0000ff;">="5,0,0,0" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;"> </span><span style="color:#a31515;">TextBlock</span><span style="color:#0000ff;">&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;"> </span><span style="color:#a31515;">DataTemplate</span><span style="color:#0000ff;">&gt;</span></span></span></span></span></span></span></code></p>
<p><code> </code></p>
<p><code><span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">DataTemplate</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">="DefenderTemplate"&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Foreground</span><span style="color:#0000ff;">="Blue"&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="Tackling Rating:" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=TacklingRating}"<br />
<span style="color:#ff0000;"> Margin</span><span style="color:#0000ff;">="5,0,5,0" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="Strength:" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=Strength}"<br />
<span style="color:#ff0000;"> Margin</span><span style="color:#0000ff;">="5,0,0,0" /&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;"> </span><span style="color:#a31515;">TextBlock</span><span style="color:#0000ff;">&gt;<br />
<span style="color:#a31515;"> </span><span style="color:#0000ff;"> </span><span style="color:#a31515;">DataTemplate</span><span style="color:#0000ff;">&gt;<br />
</span></span></span></span></span></span></span></span></span></span></code></p>
<p>And now its time to create a DataTemplateSelector which, according to the type of the item, will return the proper DataTemplate :</p>
<p><code> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">PlayerTemplateSelector</span> : <span style="color:#2b91af;">DataTemplateSelector<br />
{<br />
<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">DataTemplate</span> MidfielderTemplate { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }<br />
<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">DataTemplate</span> DefenderTemplate { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</span></code></p>
<p><code><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#2b91af;">DataTemplate</span> SelectTemplate(<span style="color:#0000ff;">object</span> item, <span style="color:#2b91af;">DependencyObject</span> container)<br />
{<br />
<span style="color:#0000ff;">if</span> (item <span style="color:#0000ff;">is</span> <span style="color:#2b91af;">Midfielder</span>)<br />
{<br />
<span style="color:#0000ff;">return</span> MidfielderTemplate;<br />
}<br />
<span style="color:#0000ff;">if</span> (item <span style="color:#0000ff;">is</span> <span style="color:#2b91af;">Defender</span>)<br />
{<br />
<span style="color:#0000ff;">return</span> DefenderTemplate;<br />
}</code></p>
<p><code> </code></p>
<p><code><span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;<br />
}<br />
}<br />
</code></p>
<p>So as you can see&#8230; we override the SelectTemplate method and provide our logic. The reason I defined two properties of DataTemplate type is so i`ll be able to link up the DataTemplates all through XAML in the following manner:</p>
<p><code><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">local</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">PlayerTemplateSelector</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">="PlayerTemplateSelector"</span> <span style="color:#ff0000;"> MidfielderTemplate</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> MidfielderTemplate</span><span style="color:#0000ff;">}"</span> <span style="color:#ff0000;"> DefenderTemplate</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> DefenderTemplate</span><span style="color:#0000ff;">}" /&gt;<br />
</span></code></p>
<p>Now all we need is to use some kind of container which allows us to set a DataTemplateSelector. Every object deriving from ContentControl allows this (Label, Button etc.) but if no special functionality is required from the control we can use a very lightweight ContentPresenter:</p>
<p><code><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ContentPresenter</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=SelectedItem, </span><span style="color:#ff0000;">ElementName</span><span style="color:#0000ff;">=MainPlayersList}"</span> <span style="color:#ff0000;"> ContentTemplateSelector</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> PlayerTemplateSelector</span><span style="color:#0000ff;">}" /&gt;<br />
</span></code></p>
<p>The Content of the ContentPresenter is bound to the SelectedItem of our list container (In this case MainPlayersList is a simple ListBox) and so whenever it changes this triggers a template selection using the specified selector.</p>
<p>We can clearly see that DataTemplateSelector is a great tool to handle display of versatile data in some region of our view. Another great use for the DataTemplateSelector is where we want to change the controls available to the user according to certain mode selections. For example: A user can decide wether to enter some string input or to enter a date, according to the user&#8217;s selection the proper control will appear to allow simple and easy data entry&#8230;</p>
<p>In conclusion, great tool which makes alot of common WPF UI tasks very simple&#8230; two thumbs up&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/amiravni.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/amiravni.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/amiravni.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=amiravni.wordpress.com&amp;blog=12336291&amp;post=3&amp;subd=amiravni&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://amiravni.wordpress.com/2010/03/02/datatemplateselector-what-the-hell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5a027eb1a2358e9e3561b7494aec51e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">amiravni</media:title>
		</media:content>
	</item>
	</channel>
</rss>
