Loading

[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等)

posted @ 2019-01-26 13:56  李正浩  阅读(1161)  评论(0编辑  收藏  举报