这个问题在这里已有答案:
当鼠标移过按钮时,我正在努力改变按钮的默认行为。以下是代码的一部分:(我省略了各种面板的代码,只保留了感兴趣的面板的代码)
<Window.Resources>
<Style x:Key="leftPanelButtons" TargetType="{x:Type Button}">
.... various setters go here ....
<Setter Property="Background" Value="ForestGreen"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkSeaGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<DockPanel Grid.Row="2">
<DockPanel Height="Auto" Width="Auto" VerticalAlignment="Stretch">
<UniformGrid Columns="1" Height="Auto" VerticalAlignment="Stretch" MinWidth="150" Width="Auto">
<UniformGrid.Resources>
<Style BasedOn="{StaticResource leftPanelButtons}" TargetType="Button"/>
</UniformGrid.Resources>
<Button>
button1
</Button>
<Button>
button2
</Button>
<Button>
button3
</Button>
</UniformGrid>
<StackPanel Height="Auto" Width="Auto" Background="PaleGoldenRod">
</StackPanel>
</DockPanel>
</DockPanel>
我希望将按钮背景颜色设置为ForestGreen,并在鼠标移过按钮时更改为DarkSeaGreen。我确实得到了ForestGreen颜色,但是当我将鼠标移到按钮上时,颜色变为浅蓝色变体。我有点复制了这个page的代码。
是的,这有点奇怪。要修改MouseOver上按钮的背景,您实际上必须更改ControlTemplate。看这里: