在MouseEnter事件上为TextBlock添加下划线,可以通过使用TextBlock的TextDecorations属性来实现。以下是一个简单的示例,展示了如何在TextBlock上添加下划线:
<TextBlock Text="Hello, world!" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"/>
在代码后台中,我们需要添加两个事件处理程序,一个用于MouseEnter事件,另一个用于MouseLeave事件。这些事件处理程序将分别添加和删除下划线:
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
TextBlock textBlock = sender as TextBlock;
textBlock.TextDecorations = TextDecorations.Underline;
}
private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
TextBlock textBlock = sender as TextBlock;
textBlock.TextDecorations = null;
}
这样,当鼠标悬停在TextBlock上时,它将显示下划线,当鼠标离开时,下划线将被删除。
领取专属 10元无门槛券
手把手带您无忧上云