C# 更改按钮';鼠标盖上的背景
当鼠标悬停在两个按钮上时,我必须更改两个按钮的颜色,我已经搜索并遵循了许多教程,但无法使其工作 这是应用的样式:C# 更改按钮';鼠标盖上的背景,c#,wpf,C#,Wpf,当鼠标悬停在两个按钮上时,我必须更改两个按钮的颜色,我已经搜索并遵循了许多教程,但无法使其工作 这是应用的样式: <Window.Resources> <Style x:Key="btnStyleBase" TargetType="Button"> <Setter Property="Background" Value="Transparent"/> <Setter Property="FontFamily" Va
<Window.Resources>
<Style x:Key="btnStyleBase" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FontFamily" Value="{StaticResource FontAwesome}"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Width" Value="42"/>
<Setter Property="Height" Value="40"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="btnStyleClose" TargetType="{x:Type Button}" BasedOn="{StaticResource btnStyleBase}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
以下是实现这些样式的按钮:
<Button Name="btn1" Style="{StaticResource btnStyleBase}" Click="..." />
<Button Name="btn2" Style="{StaticResource btnStyleClose}" Click="..." />
“IsMouseOver”属性被触发,但即使它可以应用任何其他设置器,按钮的背景仍保持为默认的浅蓝色我认为它正在查找名为
按钮的属性
以及名为背景
的子属性。将按钮。背景
更改为背景
,您应该身体状况良好:
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
默认情况下,WPF控件有一个在后面的操作系统中定义的控件模板
与Web开发一样,每当您在不同版本的Microsoft windows(Win7、Win8、Win10等)上运行应用程序时,您将看到每个控件的不同显示
如果要消除这些更改,必须为每个控件重写ControlTemplate
对于按钮,您可以使用此按钮(并根据需要进行更改):
[此模板还具有关于颜色更改的动画,您可以对其进行自定义]
<Style TargetType="Button" x:Key="ButtonBaseStyle">
<Setter Property="Padding" Value="12,6"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
<Style TargetType="Button" x:Key="PrimaryButton" BasedOn="{StaticResource ButtonBaseStyle}">
<Setter Property="BorderBrush" Value="#2e6da4"/>
<Setter Property="Background" Value="#337ab7"/>
<Setter Property="Foreground" Value="#fff"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Name="container" Cursor="Hand" Padding="{TemplateBinding Padding}"
BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter ContentSource="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState Name="Normal">
</VisualState>
<VisualState Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#286090"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#204d74"></ColorAnimation>
</Storyboard>
</VisualState>
<VisualState Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#204d74"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#122b40"></ColorAnimation>
</Storyboard>
</VisualState>
<VisualState Name="Disabled">
<Storyboard>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#337ab7"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
Duration="0:0:0.02" To="#2e6da4"></ColorAnimation>
<DoubleAnimation Storyboard.TargetName="container" Storyboard.TargetProperty="Opacity"
Duration="0:0:0.02" To="0.8"></DoubleAnimation>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有一些开源解决方案,比如Bootstrap WPF
和Material Design WPF
,您可以阅读和更改它们。即使更改它们,您也会在Win7的鼠标上方看到浅蓝色
,但我不记得用这种方法遇到了操作系统特有的问题。。。因为这是一个10秒的修复,你可能想在重写整个模板之前至少测试一下这种方法。在更改操作系统版本时,其他控件中的控件边距和填充也会发生变化。Microsoft没有花更多的时间为WPF创建主题。我不是质疑默认的XP按钮与Windows 7按钮的外观不同,我只是质疑您无法在Windows 7中自定义控件属性的前提。您应该能够更改其中一个属性的颜色,即使默认情况下其他属性看起来有点不同。这些差异提供了符合操作系统的“本机”外观,而如果您需要按钮在所有平台上看起来完全相同,则定制按钮模板更好(尽管它们会与操作系统中的所有其他按钮看起来不同)实际上,@Fehu的主要问题是识别Style.Triggers中的所有条件。如果知道按钮默认设置中的所有触发器,则可以对其进行自定义。你是对的,但我更愿意防止将来的bug(最好说:Chages
)。所以在这种情况下,我正在搜索一个控件模板。明白了!我也会搜索引导wpf