Declaring Text Decorations such as Underline, Strikethrough in a Style
Asked Answered
B

1

39

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.

Benedictine answered 16/2, 2009 at 4:59 Comment(0)
V
59

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>
Vitreous answered 16/2, 2009 at 5:9 Comment(4)
+1 - I think it's just the fact that @Benedictine is using "Control.TextDecorations" rather than "TextBlock.TextDecorations".Sage
@Matt, half right, I was also applying that Style to a Label control, no underlining was displayed. When I changed to a TextBlock it is displayed???Benedictine
Hi how to apply StrikeThrough to text of a textblock??? is there any property for that in Silverlight 4.0?Holo
Same as Underline... <TextBlock Text="Some Text" TextDecorations="Strikethrough" />Mush

© 2022 - 2024 — McMap. All rights reserved.