Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF样式按钮_C#_Wpf_Xaml_Styles - Fatal编程技术网

C# WPF样式按钮

C# WPF样式按钮,c#,wpf,xaml,styles,C#,Wpf,Xaml,Styles,我试图改变鼠标悬停事件的风格 以下是我的风格代码: <Window.Resources> <Style x:Key="NavigationButton" TargetType="{x:Type Button}"> <Setter Property="Background" Value="#295fa6"/> <Setter Property="BorderBrush" Value="{x:N

我试图改变鼠标悬停事件的风格

以下是我的风格代码:

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


,因为默认模板使用动画(固定值)来更改背景,并且该动画在中具有几乎最高的优先级。为此,您需要更改默认模板。答案已经在:此外,buttons应用了一个默认控件模板,鼠标悬停行为就来自于此。你可能想进一步了解它。