这是我的XAML,由于某种原因,它没有在鼠标悬停时改变颜色,所以我为其添加了触发器,并且当将鼠标悬停在按钮上却根本不执行任何操作时,我认为可以这样做,为什么呢?
<Button Click="ButtonBase_OnClick" Margin="10,0,0,0" Style="{DynamicResource RoundedButtonStyle}" Width="100" Height="30" HorizontalAlignment="Left">
<Grid>
<Image IsHitTestVisible="False" Height="15" Width="15" VerticalAlignment="Center" HorizontalAlignment="Left" Source="Resources/addButton.png" />
<TextBlock IsHitTestVisible="False" Margin="20,0,0,1" VerticalAlignment="Center" Foreground="#9e9e9e">Add Product</TextBlock>
</Grid>
</Button>
<Window.Resources>
<Style x:Key="RoundedButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="5" Background="#2d2d30" BorderThickness="1" Padding="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
答案 0 :(得分:2)
它没有改变,因为您将Button
的控制模板修改为:
<Border CornerRadius="5" Background="#2d2d30" BorderThickness="1" Padding="2"
请注意硬编码的背景色。无论您对按钮的Background
属性执行什么操作,它都不会生效,因为它没有被使用。如果要在模板中使用该属性,则需要TemplateBinding
:
Background="{TemplateBinding Background}"
然后使用该样式将默认值设置为原始值。
<Setter Property="Background" Value="#2d2d30"/>