The stuff that does work
I need to style controls of a certain type that are children of a StackPanel. I'm using:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">...</Style>
</StackPanel.Resources>
<TextBlock ...>
...
</StackPanel>
And this works fine! Each TextBlock looks to the resources of it's parent (the StackPanel) to find out how it should be styled. It doesn't matter how far down you nest the TextBlock down a StackPanel... if it doesn't find a style in its direct parent, it will look at its parent's parent and so on, until it finds something (in this case, the style that was defined in ).
The stuff that doesn't work
I ran into a problem when I nested a TextBlock inside a ContentControl, which had a Template (see code below). The ControlTemplate seems to disrupt the way a TextBlock retrieves its style from its parents, grandparents,...
The use of a ControlTemplate effectively seems to knock out cold the TextBlock's means of finding its rightful style (the one in StackPanel.Resources). When it encounters a ControlTemplate, it stops looking for its style in the resources up the tree, and instead defaults to the style in MergedDictionaries of the Application itself.
<StackPanel Orientation="Vertical" Background="LightGray">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
</Style>
</StackPanel.Resources>
<TextBlock Text="plain and simple in stackpanel, green" />
<ContentControl>
<TextBlock Text="inside ContentControl, still green" />
</ContentControl>
<ContentControl>
<ContentControl.Template>
<ControlTemplate TargetType="{x:Type ContentControl}">
<StackPanel Orientation="Vertical">
<ContentPresenter />
<TextBlock Text="how come this one - placed in the template - is not green?" />
</StackPanel>
</ControlTemplate>
</ContentControl.Template>
<TextBlock Text="inside ContentControl with a template, this one is green as well" />
</ContentControl>
</StackPanel>
Is there a way - besides duplicating the Style in StackPanel.Resources to ControlTemplate.Resources - to make the TextBlock inside that ControlTemplate find the defined style?
Thanks...
WPF considers
ControlTemplates
to be a boundry, and will not apply implicit styles (styles without anx:Key
) inside of templates.But there is one exception to this rule: anything that inherits from
Control
will apply implicit styles.So you could use a
Label
instead of aTextBlock
, and it would apply the implicit style defined further up your XAML hierarchy, however sinceTextBlock
inherits fromFrameworkElement
instead ofControl
, it won't apply the implicit style automatically and you have to add it manually.My most common way to get around this is to add an implicit style in the
ControlTemplate.Resources
that isBasedOn
the existing implicitTextBlock
styleOther common ways of getting around this are:
Place the implicit style in
<Application.Resources>
. Styles placed here will apply to your entire application, regardless of template boundaries. Be careful with this though, as it will apply the style toTextBlocks
inside of other controls as well, like Buttons or ComboBoxesUse a
Label
instead of aTextBlock
since it's inherited fromControl
, so will apply implicit Styles defined outside theControlTemplate
Give the base style an
x:Key
and use it as the base style for an implicitTextBlock
styles inside theControlTemplate
. It's pretty much the same as the top solution, however it's used for base styles that have anx:Key
attribute