Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

wpf - Declaring Text Decorations such as Underline, Strikethrough in a Style

How do I include text decorations such as Underline, Strikethrough etc in a Style definition:

<Style x:Key="UnderlinedLabel">
     <Setter Property="Control.FontFamily" Value="Trebuchet MS" />
     <Setter Property="Control.FontSize" Value="14" />
     <!-- Next line fails -->
     <Setter Property="Control.TextDecorations" Value="Underline" />
</Style>

I'm familiar with using the following XAML to underline text:

<TextBlock>
   <Underline>
       Underlined text
   </Underline>
</TextBlock>

However text decoration is just another style, I want to be able to define it declaritively like FontWeight, FontSize etc.

[Update]

I was applying this style to a Label control. This was my main problem. It appears you can't underline text in a Label. Change to a TextBlock (thanks gix) and all is well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Underlining text can be done either with <Underline>...</Underline> or with the TextDecorations attribute set to Underline. You can include the latter in a style definition:

<Style x:Key="Underlined">
    <Setter Property="TextBlock.TextDecorations" Value="Underline" />
</Style>

<TextBlock Style="{StaticResource Underlined}">
    Foo
</TextBlock>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...