wpf 在样式中声明文本装饰,例如下划线、删除线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/552303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring Text Decorations such as Underline, Strikethrough in a Style
提问by Ash
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:
我熟悉使用以下 XAML 为文本加下划线:
<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.
然而文本装饰只是另一种风格,我希望能够像 FontWeight、FontSize 等一样声明性地定义它。
[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.
我正在将此样式应用于 Label 控件。这是我的主要问题。看来您不能在标签中为文本加下划线。更改为 TextBlock(感谢 gix),一切都很好。
回答by gix
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:
可以使用<Underline>...</Underline>
或 将TextDecorations
属性设置为来完成下划线文本Underline
。您可以在样式定义中包含后者:
<Style x:Key="Underlined">
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
</Style>
<TextBlock Style="{StaticResource Underlined}">
Foo
</TextBlock>