问题
I got a code that change the color to yellow of a button when i pressed, the problem is not going back to original color when i release, any solution?
<Button Content="0" FontSize="28" FontWeight="Bold" Height="53" HorizontalAlignment="Left" Margin="67,51,0,0" Name="button10" VerticalAlignment="Top" Width="63" Grid.Column="9" Grid.Row="5" Grid.ColumnSpan="3" Grid.RowSpan="2">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Yellow"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
回答1:
If you're not actually doing any animation, you can dispense with the storyboard:
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Yellow" />
</Trigger>
</Style.Triggers>
</Style>
This will revert to the previous background color when the condition is no longer true. But it only works if the existing background color is set by a Style Setter -- if you put in a Background="Purple"
attribute on the Button, the style can't touch the background color.
回答2:
How about
<Trigger Property="IsPressed" Value="False">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="OriginalColor"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
来源:https://stackoverflow.com/questions/36631488/wpf-app-c-change-color-only-on-pressed-event-and-back-to-original-color-on-re