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?
1: <UserControl x:Class="DataTemplatesImplicitStyles.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8: <UserControl.Resources>
9: <Style TargetType="TextBlock">
10: <Setter Property="Foreground" Value="Red"/>
11: </Style>
12: <Style TargetType="TextBlock" x:Key="RedText">
13: <Setter Property="Foreground" Value="Red"/>
14: </Style>
15: </UserControl.Resources>
16: <Grid x:Name="LayoutRoot" Background="White">
17: <TextBlock Text="My Red Text"></TextBlock>
18: </Grid>
19: </UserControl>
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?
1: <UserControl x:Class="DataTemplatesImplicitStyles.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8: <UserControl.Resources>
9: <Style TargetType="TextBlock">
10: <Setter Property="Foreground" Value="Red"/>
11: </Style>
12: <Style TargetType="TextBlock" x:Key="RedText">
13: <Setter Property="Foreground" Value="Red"/>
14: </Style>
15: </UserControl.Resources>
16: <Grid x:Name="LayoutRoot" Background="White">
17: <Button>
18: <Button.Template>
19: <ControlTemplate TargetType="Button">
20: <TextBlock Text="My Button Text"></TextBlock>
21: </ControlTemplate>
22: </Button.Template>
23: </Button>
24: </Grid>
25: </UserControl>
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?
1: <UserControl x:Class="DataTemplatesImplicitStyles.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8: <UserControl.Resources>
9: <Style TargetType="TextBlock">
10: <Setter Property="Foreground" Value="Red"/>
11: </Style>
12: <Style TargetType="TextBlock" x:Key="RedText">
13: <Setter Property="Foreground" Value="Red"/>
14: </Style>
15: </UserControl.Resources>
16: <Grid x:Name="LayoutRoot" Background="White">
17: <ItemsControl ItemsSource="randomstring">
18: <ItemsControl.ItemTemplate>
19: <DataTemplate>
20: <StackPanel Orientation="Horizontal">
21: <TextBlock Text="Implicit: " Margin="4"></TextBlock>
22: <TextBlock Text="{Binding}" Margin="4"></TextBlock> 23:
24: <TextBlock Text="Explicit: " Margin="4"></TextBlock>
25: <TextBlock Text="{Binding}" Margin="4" Style="{StaticResource RedText}"></TextBlock> 26: </StackPanel>
27: </DataTemplate>
28: </ItemsControl.ItemTemplate>
29: </ItemsControl>
30: </Grid>
31: </UserControl>
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?
1: <UserControl x:Class="DataTemplatesImplicitStyles.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8: <UserControl.Resources>
9: <Style TargetType="Button">
10: <Setter Property="Foreground" Value="Red"></Setter>
11: </Style>
12: </UserControl.Resources>
13: <Grid x:Name="LayoutRoot" Background="White">
14: <ItemsControl ItemsSource="randomstring">
15: <ItemsControl.ItemTemplate>
16: <DataTemplate>
17: <Button Content="{Binding}"></Button> 18: </DataTemplate>
19: </ItemsControl.ItemTemplate>
20: </ItemsControl>
21: </Grid>
22: </UserControl>
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.
1: <UserControl x:Class="DataTemplatesImplicitStyles.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8: <UserControl.Resources>
9: <Style TargetType="TextBlock" x:Key="RedText">
10: <Setter Property="Foreground" Value="Red"/>
11: </Style>
12: </UserControl.Resources>
13: <Grid x:Name="LayoutRoot" Background="White">
14: <ItemsControl ItemsSource="randomstring">
15: <ItemsControl.ItemTemplate>
16: <DataTemplate>
17: <StackPanel Orientation="Horizontal">
18: <StackPanel.Resources>
19: <Style TargetType="TextBlock" BasedOn="{StaticResource RedText}"></Style> 20: </StackPanel.Resources>
21: <TextBlock Text="Implicit: " Margin="4"></TextBlock>
22: <TextBlock Text="{Binding}" Margin="4"></TextBlock> 23:
24: <TextBlock Text="Explicit: " Margin="4"></TextBlock>
25: <TextBlock Text="{Binding}" Margin="4" Style="{StaticResource RedText}"></TextBlock> 26: </StackPanel>
27: </DataTemplate>
28: </ItemsControl.ItemTemplate>
29: </ItemsControl>
30: </Grid>
31: </UserControl>
And like expected, all text shows up red.
