(WPF App C#) Change color only on pressed event and back to original color on release?

余生长醉 提交于 2020-07-10 04:27:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!