我正在尝试更改按钮的背景,但问题是它具有三种状态 - IsEnabled、IsPressed 和 Clicked。在默认状态下,当我按下按钮时,背景会发生变化,然后当我单击按钮时,按钮会动画,但下次按下按钮时,背景不会改变,但 Click(动画)可以正常工作,因为有更改类似资源时发生冲突。
这是我的源代码
<Style TargetType="{x:Type Button}" x:Key="myButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="myBorder" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Name="Stack" Background="DarkGray">
<Image x:Name="checkBoxImage" Width="40" Height="40" Source="NextPage.png"/>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Stack" Storyboard.TargetProperty="Background">
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame Value="Blue1" KeyTime="0" />
<DiscreteObjectKeyFrame Value="DarkGray1" KeyTime="0:0:0.4" />
</ObjectAnimationUsingKeyFrames.KeyFrames>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Stack" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>