WPF学习笔记--样式基础
程序员文章站
2024-02-16 12:29:58
...
效果图:当鼠标放上去之后高亮显示,离开后恢复.
前台代码:
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="5"></Setter>
<EventSetter Event="MouseEnter" Handler="TextBlock_MouseEnter"></EventSetter>
<EventSetter Event="MouseLeave" Handler="TextBlock_MouseLeave"></EventSetter>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock >Hover over me.</TextBlock>
<TextBlock >Don't bother with me</TextBlock>
<TextBlock>Hover over me.</TextBlock>
</StackPanel>
后台代码:
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
((TextBlock)sender).Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
}
private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
((TextBlock)sender).Background = null;
}
上一篇: Web前端开发 北京林业大学 第3周练习