鼠标悬停时的 WPF TextBlock 样式下划线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13275765/
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
WPF TextBlock Style Underline on Mouse Hover
提问by Mike de Klerk
Simple question. Can the following WPF C# code cut down some weight? I mean, WTF, uh... I mean WPF, come one. Have you every seen CSS? I only want to underline the Text when I hover with the mouse just like a link. Do I really have to add 9 lines for that?
简单的问题。下面的 WPF C# 代码可以减少一些重量吗?我的意思是,WTF,呃...我的意思是 WPF,来一个。你见过 CSS 吗?当我像链接一样用鼠标悬停时,我只想在文本下划线。我真的必须为此添加 9 行吗?
<TextBlock x:Name="Cassette_tblPrintLabel" Text="Print Label" Canvas.Left="154" Canvas.Top="215" Foreground="#FF3355FF" Cursor="Hand" MouseDown="Cassette_lblPrintLabel_MouseDown">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.TextDecoration" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Thanks in advance!
提前致谢!
回答by McGarnagle
Add the style as resource; then at least you can re-use it. I think that's the best you can do.
将样式添加为资源;那么至少你可以重新使用它。我认为这是你能做的最好的事情。
<Application.Resources>
<Style TargetType="TextBlock" x:Key="HoverUnderlineStyle">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
<TextBlock Style="{StaticResource HoverUnderlineStyle}" />

