<?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>Joep on technology</title>
	<atom:link href="http://joepb.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://joepb.wordpress.com</link>
	<description>just another techy geek blog</description>
	<lastBuildDate>Fri, 02 Jan 2009 08:30:38 +0000</lastBuildDate>
	<language>nl</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='joepb.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Joep on technology</title>
		<link>http://joepb.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://joepb.wordpress.com/osd.xml" title="Joep on technology" />
	<atom:link rel='hub' href='http://joepb.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Setting styles for basetypes in WPF</title>
		<link>http://joepb.wordpress.com/2009/01/02/setting-styles-for-basetypes-in-wpf/</link>
		<comments>http://joepb.wordpress.com/2009/01/02/setting-styles-for-basetypes-in-wpf/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 08:14:16 +0000</pubDate>
		<dc:creator>joepb</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 3.0]]></category>
		<category><![CDATA[.net 3.5]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://joepb.wordpress.com/?p=24</guid>
		<description><![CDATA[Recently I stumbled across a question on Experts Exchange, where someone wanted to know why he couldn&#8217;t change the default style for the baseclass of his WPF-control, and get these changes reflected in the final instance. Assuming more people must struggle with the problem (heck I know I have for a very long time), I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joepb.wordpress.com&amp;blog=4135787&amp;post=24&amp;subd=joepb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I stumbled across a <a href="http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/Q_23985446.html" target="_blank">question</a> on <a href="http://www.experts-exchange.com" target="_blank">Experts Exchange</a>, where someone wanted to know why he couldn&#8217;t change the default style for the baseclass of his WPF-control, and get these changes reflected in the final instance. Assuming more people must struggle with the problem (heck I know I have for a very long time), I decided to explain the mechanism here too, for everyone who wants to know.</p>
<p>Assume we created a custom BaseControl with a DerivedControl.</p>
<pre class="brush: csharp;">
  public class BaseControl : Control
  {
    static BaseControl()
    {
      DefaultStyleKeyProperty.OverrideMetadata(
        typeof(BaseControl),
        new FrameworkPropertyMetadata(typeof(BaseControl)));
    }
  }
  public class DerivedControl : BaseControl
  {
    static DerivedControl()
    {
      DefaultStyleKeyProperty.OverrideMetadata(
        typeof(DerivedControl),
        new FrameworkPropertyMetadata(typeof(DerivedControl)));
    }
  }
</pre>
<p>And we set the generic styles like so:</p>
<pre class="brush: xml;">
  &lt;Style TargetType=&quot;{x:Type local:BaseControl}&quot;&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
      &lt;Setter.Value&gt;
        &lt;ControlTemplate TargetType=&quot;{x:Type local:BaseControl}&quot;&gt;
          &lt;Border Background=&quot;{TemplateBinding Background}&quot;&gt;
            &lt;TextBlock&gt;Hello World!&lt;/TextBlock&gt;
          &lt;/Border&gt;
        &lt;/ControlTemplate&gt;
      &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;Style TargetType=&quot;{x:Type local:DerivedControl}&quot;&gt;
    &lt;Setter Property=&quot;Background&quot; Value=&quot;Violet&quot;/&gt;
  &lt;/Style&gt;
</pre>
<p>We created a BaseControl-style with a template with some text in it, and a DerivedControl-style giving it&#8217;s background some color.<br />
If we run a simple test with a form containing a DerivedControl, we want to have a text with a violet background. But what do we get?</p>
<p><img class="size-full wp-image-30 alignnone" title="basestylingsample-ss01" src="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss01.png?w=124&#038;h=100" alt="basestylingsample-ss01" width="124" height="100" /></p>
<p>Exactly: an empty window. Not at all what we expected.</p>
<p>The problem lies in an incorrect assumption we make, because we are so used to the concept of inheritance and polymorphism. We created two styles with two different keys ({x:Type local:BaseControl} and {x:Type local:DerivedControl}). In the code of BaseControl we specify with the OverrideMetadata-method that we want the system to look for a style with a key equal to &#8216;typeof(BaseControl)&#8217;. In the code of DerivedControl we again override that metadata to make the system look for a style with a key equal to &#8216;typeof(DerivedControl)&#8217;. Since we <em>override</em> the metadata, the base-value is lost.</p>
<p>When the system want to construct a DerivedControl it reads the DefaultStyleKeyProperty from the control and gets a &#8216;typeof(DerivedControl)&#8217;. This is just a key, we could just as easily have given it a string reading &#8220;DerivedControl&#8221; to look for. The resource-resolver starts to try and find resources with the key &#8216;typeof(DerivedControl)&#8217;. It is to be expected that an resource with the key &#8216;typeof(BaseControl)&#8217; will not be found, since the resolver only checks for equality. To prove that, you can check with <a href="http://www.red-gate.com/products/reflector/">Reflector</a> and look at the internal method System.Windows.FrameworkContentElement.FindResourceOnSelf() where you can see it checks wether a resource exists using &#8216;dictionary.Contains(resourceKey)&#8217;.</p>
<p><img class="alignright size-full wp-image-31" title="basestylingsample-ss02" src="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss02.png?w=304&#038;h=96" alt="basestylingsample-ss02" width="304" height="96" />In the debugger screen to the right you can see the derivedControl instance does get its background set, but not the template. The control does get it&#8217;s own style applied, setting its BackGround to Violet, but there is no ControlTemplate to show us the color, because that&#8217;s in the style for the BaseControl!</p>
<p>Resolving this is easier than it seems, but it needs manual work. In the style for the DerivedControl we can add a BasedOn property. The xaml will have to read like this:</p>
<pre class="brush: xml;">
  &lt;Style TargetType=&quot;{x:Type local:DerivedControl}&quot; BasedOn=&quot;{StaticResource {x:Type local:BaseControl}}&quot;&gt;
    &lt;Setter Property=&quot;Background&quot; Value=&quot;Violet&quot;/&gt;
  &lt;/Style&gt;
</pre>
<p>Using that property we can tell the style it should first apply the BasedOn-style and afterwards apply it&#8217;s own setters. (We need the &#8216;StaticResource&#8217; part because the BasedOn-property takes a Style, not a key.) Now our window looks like this:</p>
<p><img class="alignnone size-full wp-image-34" title="basestylingsample-ss03" src="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss03.png?w=124&#038;h=100" alt="basestylingsample-ss03" width="124" height="100" /></p>
<p>Admitted, it is one <em>ugly </em>window, but at least it shows the way we told it to.</p>
<p>I personally think it is good practice to <em>always</em> set the BasedOn property for your styles to the style of the baseclass, and giving all baseclasses at least empty styles (without the empty style the StaticResource will fail to build).</p>
<p>Too bad the WPF-team didn&#8217;t really follow the BasedOn-principle; then we could have customized for example the style for System.Windows.Controls.Control, potentially triggering on all kinds of properties and see that style reflected in all derived controls. But alas, we have to use tricks involving attached properties to get things like that to work. I hope to post on that a little later on.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joepb.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joepb.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joepb.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joepb.wordpress.com&amp;blog=4135787&amp;post=24&amp;subd=joepb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joepb.wordpress.com/2009/01/02/setting-styles-for-basetypes-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aede72e2d72d9992ec291b99b3eb56f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Joep.B</media:title>
		</media:content>

		<media:content url="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss01.png" medium="image">
			<media:title type="html">basestylingsample-ss01</media:title>
		</media:content>

		<media:content url="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss02.png" medium="image">
			<media:title type="html">basestylingsample-ss02</media:title>
		</media:content>

		<media:content url="http://joepb.files.wordpress.com/2009/01/basestylingsample-ss03.png" medium="image">
			<media:title type="html">basestylingsample-ss03</media:title>
		</media:content>
	</item>
		<item>
		<title>PRINT &#8220;Hello World!&#8221;</title>
		<link>http://joepb.wordpress.com/2008/12/26/print-hello-world/</link>
		<comments>http://joepb.wordpress.com/2008/12/26/print-hello-world/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 10:35:19 +0000</pubDate>
		<dc:creator>joepb</dc:creator>
				<category><![CDATA[blogs]]></category>

		<guid isPermaLink="false">http://joepb.wordpress.com/?p=11</guid>
		<description><![CDATA[Having thought about it, and lingering in a state of &#8220;I&#8217;m going to do it soon&#8221; a long time, it now really is time that I started a blog. So here it is: my latest (and first) blog &#8220;Joep on Technology&#8221;. I&#8217;m interested in lost of technogical topics, but mainly my experience lies at the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joepb.wordpress.com&amp;blog=4135787&amp;post=11&amp;subd=joepb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Having thought about it, and lingering in a state of &#8220;I&#8217;m going to do it soon&#8221; a long time, it now really is time that I started a blog.</p>
<p>So here it is: my latest (and first) blog <em>&#8220;Joep on Technology&#8221;</em>.</p>
<p>I&#8217;m interested in lost of technogical topics, but mainly my experience lies at the development of applications for Microsoft platforms. That&#8217;s why I&#8217;ve decided to write my blog in English, instead of my native language Dutch. I&#8217;m hoping that my English blogs will be read by more people, considering that most developers are used to reading English documentation, regardless of their native language. I started a blog in Dutch half a year ago, but I noticed the fact i had to blog in Dutch was really a threshold for me.</p>
<p><em>So, Joep, what can we expect from your blog?</em></p>
<p>Well, frankly, I&#8217;m not sure yet either. I&#8217;m planning on just blogging on all kinds of topics I can come up with. I anticipate a few articles on hardware, electronics and stuff like that, but mainly i will be targetting .net development, c#, wpf, Windows Vista and related topics. But I guess only time will tell what this blog is going to be about. Could be anything that pops to my mind.</p>
<p>Hope to see you soon on my first topic with real content!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/joepb.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/joepb.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/joepb.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=joepb.wordpress.com&amp;blog=4135787&amp;post=11&amp;subd=joepb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://joepb.wordpress.com/2008/12/26/print-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aede72e2d72d9992ec291b99b3eb56f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Joep.B</media:title>
		</media:content>
	</item>
	</channel>
</rss>
