欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF学习笔记--样式基础

程序员文章站 2024-02-16 12:29:58
...

效果图:当鼠标放上去之后高亮显示,离开后恢复.

WPF学习笔记--样式基础

前台代码:

<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;
        }

 

相关标签: WPF