Boris B.
Boris B.

Reputation: 5024

Use same style for TextBlock and Run element

I have a WPF style that sets TextDecorations dependency property for TargetType: TexBlock. I need to use the same style for certain Run elements which are in some TextBlock which doesn't itself use the aforementioned style. How can I achieve this without repeating the same style with just a different TargetType?

Upvotes: 5

Views: 1886

Answers (1)

brunnerh
brunnerh

Reputation: 184526

Just do not specify a TargetType but qualify the property, e.g.:

<Style x:Key="CommonStyle">
    <Setter Property="Inline.TextDecorations" Value="StrikeThrough" />
</Style>
<TextBlock Style="{StaticResource CommonStyle}" Text="Lorem Ipsum" />
<TextBlock>
    <Run Style="{StaticResource CommonStyle}" Text="Lorem" />
    <Run Text="Ipsum" />
</TextBlock>

If you want to further develop the style you can use BasedOn, this also allows implicit application of said style by not setting a key on the derived style.

Upvotes: 5

Related Questions