C# 当鼠标悬停在按钮上时,按钮颜色不变
我已经编写了下面的XAML代码。我添加了样式代码,以在鼠标悬停在按钮上时更改按钮的颜色。但是,当鼠标悬停在按钮上时,虽然有边框效果,但背景并没有变为红色。请给我一些建议C# 当鼠标悬停在按钮上时,按钮颜色不变,c#,wpf,button,mouseover,C#,Wpf,Button,Mouseover,我已经编写了下面的XAML代码。我添加了样式代码,以在鼠标悬停在按钮上时更改按钮的颜色。但是,当鼠标悬停在按钮上时,虽然有边框效果,但背景并没有变为红色。请给我一些建议 <Window.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> <Style x:Key="MyButtonStyle"
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="1"
BorderBrush="DarkGray"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Button Style="{StaticResource MyButtonStyle}" Content="Install" Command="{Binding InstallCommand}" Visibility="{Binding InstallEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="150,30,30,22" Width="118" BorderBrush="#FF252424" FontSize="18" FontWeight="Bold" Foreground="White" FontFamily="Segoe UI Light" FontStretch="ExtraExpanded" Background="#FF4F4F4F"/>
您的代码几乎正常工作,请执行以下操作以使其完整:
- 您正在绑定样式,因此如果需要外接程序样式,则无需随按钮指定任何其他样式
- 从按钮标签中删除
、边框刷
和前景
背景
- 在setter中添加“Background”属性,然后运行应用程序并查看更改
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<!--Sets the initial Foreground color-->
<Setter Property="Foreground" Value="White"/>
<!--Sets the initial Background color-->
<Setter Property="Background" Value="Gray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="1"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource MyButtonStyle}" Content="Install"
Margin="150,30,30,22" Width="118" FontSize="18"
FontWeight="Bold" FontFamily="Segoe UI Light" FontStretch="ExtraExpanded" />
按钮控制如下:
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<!--Sets the initial Foreground color-->
<Setter Property="Foreground" Value="White"/>
<!--Sets the initial Background color-->
<Setter Property="Background" Value="Gray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="1"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource MyButtonStyle}" Content="Install"
Margin="150,30,30,22" Width="118" FontSize="18"
FontWeight="Bold" FontFamily="Segoe UI Light" FontStretch="ExtraExpanded" />