I want to have a default style for all TextBlock controls in my application that uses the Royal theme. For example:
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Margin" Value="4"/>
</Style>
This works great inherited from the Royal theme but unfortunately also affects the style of TextBlock that is used inside the templates of other controls like ComboBox and I don’t want that.
I already tried to place my style one level down (for example inside Grid.Resources) but with no luck.
Is there a way to do that? Of course I don’t want to name my style (using x:key) because I don’t want to have to explicitly apply to each and every TextBlock of my application the custom style. And without creating subclasses for overwriting the style there.
Any ideas? Thank you in advance!
P.S. I already looked at questions that might be similar but no answer there worked for me.
EDIT:
Thanks for all the answers. I hoped there is some way that I miss with which I can distinguish the TextBlock used inside other controls templates.
By this exclusion:
you pretty much ask the impossible. The style you created will be applied to all TextBlocks in the visual tree below the dictionary that contains the style.
If you want to make exceptions you will have to specify them one way or another.
Apparently your requirement “to have a default style for all TextBlock controls” isn’t true. You do NOT want ALL the TextBlocks to have this style.
Moving the style one or more levels down could be a solution if you want the style applied from that level on.
There is no way to specify that you want to apply the style to specific TextBlocks and not to others unless you are able to distinguish between them them in some way.