在样式中声明文本装饰,例如下划线,删除线

Ash*_*Ash 39 wpf xaml text styles

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

<TextBlock>
   <Underline>
       Underlined text
   </Underline>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

然而文本修饰只是另一种风格,我希望能够像FontWeight,FontSize等一样明确地定义它.

[更新]

我正在将此样式应用于Label控件.这是我的主要问题.您似乎无法在Label中为文本加下划线.更改为TextBlock(感谢gix),一切都很好.

gix*_*gix 58

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

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

<TextBlock Style="{StaticResource Underlined}">
    Foo
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • 与下划线相同...`<TextBlock Text ="Some Text"TextDecorations ="Strikethrough"/>` (4认同)
  • +1 - 我认为这只是@Ash使用"Control.TextDecorations"而不是"TextBlock.TextDecorations"的事实. (3认同)