Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use same style for TextBlock and Run element

Tags:

.net

styles

wpf

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?

like image 844
Boris B. Avatar asked Oct 20 '11 12:10

Boris B.


1 Answers

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.

like image 86
H.B. Avatar answered Nov 03 '22 22:11

H.B.