WPF 样式按钮

WPF style button

我尝试更改鼠标悬停事件的样式。

这是我的样式代码:

<Window.Resources>
    <Style x:Key="NavigationButton" 
           TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#295fa6"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="BorderBrush" Value="{x:Null}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

我就是这样用的:

<Button x:Name="test"
        Grid.Column="0"" 
        Style="{StaticResource NavigationButton}">
        <Image Source="Assets/imaaa.png" />
 </Button>

鼠标悬停仍然有默认颜色

您需要修改ControlTemplate。删除按钮上的默认行为。

<Style x:Key="NavigationButton" 
           TargetType="{x:Type Button}">
     <Setter Property="Background" Value="#295fa6"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="#295fa6">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Background" Value="Red"/>
           <Setter Property="BorderBrush" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>