2009-02-16 166 views
37

怎样包括文字装饰,例如下划线,删除线等,在样式定义:声明文本修饰,如下划线,删除线的样式

<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> 

我熟悉使用以下XAML强调文本:

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

但是文饰只是另一种风格,我希望能够declaritively定义它像fontWeight设置,字号等

[更新]

我将此样式应用于Label控件。这是我的主要问题。看起来你不能在标签中加下文字。更改为TextBlock(谢谢gix),一切都很好。

回答

54

下划线文本可以使用<Underline>...</Underline>或使用TextDecorations属性设置为Underline来完成。您可以在一个样式定义后者:

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

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

+1 - 我认为这只是@Ash使用“Control.TextDecorations”,而不是“TextBlock.TextDecorations”的事实。 – 2009-02-16 05:11:39

相关问题