Prior to Silverlight 4 implicit styles (styles that weren’t given a key) didn’t exist and all styles had to be manually applied. Naturally when I found out they were added in Silverlight 4 I was very happy, until I realized this: implicit styles defined outside of a ControlTemplate or DataTemplate won’t always apply to said template.

Given this XAML, what should it look like?

If you guess it should be the words “My Red Text” with a red foreground, you would be correct.


Now, what if I have a custom button template that uses a TextBlock?

If you guessed it was red text, you’d be wrong.


Okay, so that’s a custom button template, but what about a DataTemplate, like used in an ItemsControl?

Can you guess what happens here? In it we’re using multiple TextBlocks and also using an explicitly style (one that has a key defined). What will the above output look like?


All TextBlocks without a Style defined have default styling. If the implicit styling was working correctly, every bit of text above would be red (because they’re all TextBlocks).

Okay, all we’ve tested so far are TextBlock controls. What about a Button, will it not work there as well?

Using our past experience, we can guess that the implicit style won’t work here either.


It works for Button but not TextBlock? What is going on here? A template is considered an encapsulation boundary in Silverlight. When looking up a resource, anything that is not derived from Control will stop at this boundary when looking up implicit styles. Anything that is Control-derived will continue above the template boundary while looking up implicit resources.

This has been commented on by Microsoft, although the original Connect bug it was commented on seems to have disappeared, nor can I find anything related to this in documentation. The issue I take with this notion of encapsulation for implicit styles is that it doesn’t encapsulate against explicit styles. From a template if I have an explicitly defined style, I’m not “protected” from those styles like I am with implicit styles.

So, what do you do about it? Define all implicit styles inside your template.

And like expected, all text shows up red.