WPF TextBlock Style Underline on Mouse Hover

混江龙づ霸主 提交于 2019-12-21 08:23:01

问题


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?

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


回答1:


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}" />


来源:https://stackoverflow.com/questions/13275765/wpf-textblock-style-underline-on-mouse-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!