简体   繁体   English

如何在MouseEnter上为TextBlock加下划线

[英]How to underline a TextBlock on a MouseEnter

In a WPF form, I have the following TextBlock. 在WPF表单中,我有以下TextBlock。 When I move my mouse over it, I would like to see the text of the TextBlock underlined. 当我将鼠标移到它上面时,我希望看到TextBlock的文本加下划线。 How can I do that? 我怎样才能做到这一点? I tried with TextBlock.Triggers, but it didn't work. 我尝试使用TextBlock.Triggers,但它没有用。

Thanks! 谢谢!

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.) (为简洁起见,样式显示为内联;如果您计划重复使用,则将其提取到资源中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM