39

如何在样式定义中包含文本装饰,例如下划线、删除线等:

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

然而文本装饰只是另一种风格,我希望能够像 FontWeight、FontSize 等声明性地定义它。

[更新]

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

4

1 回答 1

59

可以使用<Underline>...</Underline>或将TextDecorations属性设置为 来为文本添加下划线Underline。您可以将后者包含在样式定义中:

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

<TextBlock Style="{StaticResource Underlined}">
    Foo
</TextBlock>
于 2009-02-16T05:09:43.963 回答