(WPF App C#)仅在按下的事件上更改颜色,并在发布时更改为原始颜色?

时间:2016-04-14 18:45:43

标签: c# wpf visual-studio-2010

当我按下时,我得到了一个代码,将按钮的颜色更改为黄色,当我发布任何解决方案时,问题不会恢复原来的颜色?

<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>

2 个答案:

答案 0 :(得分:1)

如果你实际上没有做任何动画,你可以省去故事板:

<Style TargetType="Button">
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="Yellow" />
         </Trigger>
     </Style.Triggers>
 </Style>

当条件不再为真时,这将恢复为先前的背景颜色。但它仅在样式设置器设置现有背景颜色时才有效 - 如果在Button上放置Background="Purple"属性,则样式不能触摸背景颜色。

答案 1 :(得分:0)

怎么样

<Trigger Property="IsPressed" Value="False">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="OriginalColor"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>