• [WPF]鼠标移动到Button颜色改变效果设置


    代码

     <Style x:Key="Button_Menu" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="24" />
            <Setter Property="Height" Value="24" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="MyBackgroundElement" BorderBrush="{DynamicResource ForgroundBrush}" BorderThickness="0">
                            <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="MyBackgroundElement" Property="Background" Value="{DynamicResource ForgroundBrush100}"/>
                                <Setter TargetName="MyBackgroundElement" Property="Opacity" Value="0.7"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Cursor" Value="Hand" />
        </Style>
     <Button Name="Add"  Margin="2"
                
                Style="{StaticResource Button_Menu}">
                    <iconPacks:Modern
                    Kind="PageAdd"
                    Height="18"
                    Width="18" />
                </Button>

    效果:

    注意中间一定要设置TargetName,否则可能会导致无法更改颜色。

    (代码中用到了一些自定义的颜色样式,如果要自己测试的话可以随便改成别的颜色,比如Red,Black等)

    https://github.com/li-zheng-hao
  • 相关阅读:
    通知协议KVO的用法
    UIImageView用法
    默默的学习!
    多输入参数的方法
    一本书的摘录
    实例、局部、静态变量(java)
    分数相加的例题
    类的继承
    多线程
    单例
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053592.html
Copyright © 2020-2023  润新知