C# 如何在 MouseEnter 上的 TextBlock 下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/754441/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:54:20  来源:igfitidea点击:

How to underline a TextBlock on a MouseEnter

c#wpf

提问by Martin

In a WPF form, I have the following TextBlock. When I move my mouse over it, I would like to see the text of the TextBlock underlined. How can I do that? I tried with TextBlock.Triggers, but it didn't work.

在 WPF 表单中,我有以下 TextBlock。当我将鼠标移到它上面时,我希望看到带下划线的 TextBlock 的文本。我怎样才能做到这一点?我尝试使用 TextBlock.Triggers,但没有奏效。

Thanks!

谢谢!

采纳答案by itowlson

Use a style:

使用样式:

<TextBlock Text="Hurrah">
  <TextBlock.Style>
    <Style TargetType="TextBlock">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="TextDecorations" Value="Underline" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TextBlock.Style>
</TextBlock>

(Style shown inline for brevity; extract into a resource if you're planning to reuse it.)

(为简洁起见,样式显示为内联;如果您打算重用它,请提取到资源中。)